2

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?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
4thSpace
  • 43,672
  • 97
  • 296
  • 475

3 Answers3

7

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)

Community
  • 1
  • 1
Vladimir
  • 170,431
  • 36
  • 387
  • 313
4

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];
hallski
  • 123,625
  • 4
  • 33
  • 21
  • 1
    Thanks. I suppose that is the prefix naming convention mentioned in a later posting. – 4thSpace Nov 19 '09 at 14:56
  • Exactly, and if you look at the built in classes in Cocoa they use for example NS (NextStep), CA (CoreAnimation) etc. – hallski Nov 19 '09 at 16:03
2

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
  • 2,181
  • 15
  • 16