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:
Au开发者_C百科dio 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..
精彩评论