views:

79

answers:

6

probably me be stupid, but i cannot figure out how to use namespaces in the iphone sdk. Please could somebody help me. basically I need to know where to define a namespace in the files, i assume in the header, but where? I keep getting errors.

+3  A: 

Objective-C does not have namespaces.

Daniel A. White
+3  A: 

As Daniel A. says, no namespaces in Objective-C.

It is common practice - and recommended by Apple - that you prefix your frameworks / packages with 3 letter codes. Apple uses things like *UI*Kit, *NS*String etc.

So you could use DTHCoolClass etc.

Eiko
A: 

If you're trying to use namespaced C++ code within your obj-C files then you'll need to rename your source files to tell Xcode to use the C++ compiler. Rename the relevant .m files to .mm and you should be able to use C++ namespaces.

No one in particular
+1  A: 

Objective-C has no namespaces. What people do is prefix their code with some capital letters. Like initials or product name abbreviation. Personally I hate it and I use normal names like SignUpViewController instead of SMASignUpViewController.

In my opinion the world would be a better place if app developers dropped the prefixes. I'm ok with libraries using this.

St3fan
The prefixes are clearly silly for classes that only you will ever use. If you're exporting anything or creating a library, I agree-- different story.
quixoto
+1  A: 

As others have mentioned, Objective-C doesn't have namespaces. Apple recommends that you prefix your class names with 2 or 3 letters indicating the project, framework or developer.

In practice, whether or not you should prefix depends on what sort of class you are talking about, and how it is likely to be used.

I prefix all model and utility classes, since the names could clash with Apple's (or 3rd party) libraries (e.g. use NFResourceManager instead of ResourceManager, and NFRecord instead of Record). These sort of classes may be pulled out into another project framework at some stage in the future, so prefixing them allows you to keep the names unambiguous.

I almost never prefix view controllers, unless I think there's a good chance I'll use it elsewhere, or want to share it. The majority of VCs won't get used elsewhere, so there's little benefit of prefixing (and you're unlikely to have imported code from a 3rd party library that contains VCs).

For most view classes, I prefix them, unless it's highly specific to a particular project/app, in which case there's little point.

Nick Forge
A: 

Me being a little stupid, i was trying to create the namespace in the wrong place

Daniel Thatcher