English 中文(简体)
Namespace convention for Objective-C
原标题:

I d like to group classes so they must be referenced like this:

Root.Web
Root.Mail
Root.Audio

Each of the above classes corresponds to a file:

Web.h
Mail.h
Audio.h

The goal is to use the above "Root" syntax rather than just:

Audio myAudio = [[Audio alloc] init];

Instead, it should look like:

Root.Audio myAudio = [[Root.Audio alloc] init];

Is Objective-C capable of doing that?

最佳回答

Objective-c doesn t support namespaces (as c doesn t) so you can t do that. To avoid potential collisions you can add your specific prefix to a class name

Here and here are the similar questions, may be you ll find them useful.

btw I think that if namespaces were supported they were addressed more in cpp-like way (like MyNamespace::MyClass, not MyNamespace.MyClass)

问题回答

Not to my knowledge, what you would do in Objective-C (or C) to achieve that is to namespace the class name instead.

RootAudio *myAudio = [[RootAudio alloc] init];

Although Objective-C might be capable of doing that, if you make Root a singleton with predeclared or dynamically built properties, I would really recommend against it.

It goes against the standard of Objective-C. It shouldn t be necessary to use such a method.

Every language is constructed in a certain way and should be used accordingly. The above is not how objective-c is meant to be used.

Why would you want to use it that way?

If it is so that every object can access Root.Audio, in order to play a file, then why not make Audio a singleton, or have a class method on Audio that can store and retrieve instances that you have declared? Every object in your run-time can access class methods..





相关问题
静态启动的探测阶段?

我真的想要是什么,我如何知道C++初始阶段究竟是谁?

Namespace convention for Objective-C

I d like to group classes so they must be referenced like this: Root.Web Root.Mail Root.Audio Each of the above classes corresponds to a file: Web.h Mail.h Audio.h The goal is to use the above "...

(Conventions) C# Class names

I m not too quite sure about what i should do about a grouped set of classes. My situation: I have 11 classes that relate only to the class Character.cs, but all of those classes (including ...

the compiler doesn t seem to accept Agent class

probably the answer is quite silly but I need a pair fresh of eyes to spot the problem, if you will. this is the excerpt from _tmain: Agent theAgent(void); int m = theAgent.loadSAG(); and this is ...

How to resolve Rails model namespace collision

The story so far: I have a rails app with a model named "Term". All is well until trying to install Cucumber. Upon running rake cucumber I get Term is not a class (TypeError) This happens because ...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

热门标签