views:

105

answers:

3

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?

+2  A: 

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..

nash
+4  A: 

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];
m5h
Thanks. I suppose that is the prefix naming convention mentioned in a later posting.
4thSpace
Exactly, and if you look at the built in classes in Cocoa they use for example NS (NextStep), CA (CoreAnimation) etc.
m5h
+7  A: 

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 link text 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)

Vladimir