Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one use namespaces in iOS objective-c code?

I'm writing an iOS app, "Best Korea". My organization name is "Srsly.co". I'm going to write re-usable "News" libraries that I'll use across my apps.

Each iOS app will have its own app-wide constants in a .h file, and the library code will have its constants as well in header files. I'll also have tests for each of these projects.

Is this the standard way of doing things?

In Ruby, Python, Java, etc., I'd set up namespaces along these lines:

co.srsly.bestkorea
co.srsly.bestkorea.test
co.srsly.newslib
co.srsly.newslib.test

As far as I can see, the Objective-C pattern is for each developer to choose two or three upper-case letters and prefix every class name with them.

So in my case, I'm thinking I'd choose BK as the app's classname prefix and NL for the news lib code? Am I thinking about this the right way?

EDIT: I'm considering not using namespacing at all in my application code as discussed here.

like image 739
Dogweather Avatar asked Mar 21 '13 03:03

Dogweather


People also ask

Does Objective-C have namespaces?

As you can see, Objective-C does not have a concept of namespaces. Everything is essentially file based. You basically are informing each class about what ever interfaces that have been defined in other header files. The import statement with quotes means the the header file has been defined in this project.

What is namespace in Objective-C?

There is no NameSpace in Objective-C as you are expecting in Java. Objective-C uses class Prefix like NS, UI, CG, CF etc to safely remove name space collision.

Does Swift support namespaces?

Even though Swift doesn't support namespaces within modules, there are a few viable solutions to this problem. The first solution uses structs to create namespaces and it looks something like this. We define a struct, API , and declare one or more static constant properties.

What are namespaces useful for?

Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries. All identifiers at namespace scope are visible to one another without qualification.


2 Answers

You're correct that Objective-C doesn't have built in support for namespaces, and the common solution is to use uppercase prefixes on each class. Note that Apple has stated that two letter prefixes are reserved for their use, so you should use three letter prefixes for your own classes. Otherwise, your suggested approach is the normal thing to do.

like image 183
Andrew Madsen Avatar answered Oct 17 '22 16:10

Andrew Madsen


There is no NameSpace in Objective-C as you are expecting in Java.

Objective-C uses class Prefix like NS, UI, CG, CF etc to safely remove name space collision.

And it would be better to use 3 letter Prefix for your class.

You should read this : What is the best way to solve an Objective-C namespace collision?

like image 27
Anoop Vaidya Avatar answered Oct 17 '22 18:10

Anoop Vaidya