12

Not always but most of the time, you will find iOS or Mac OS X open-source projects with names starting with the initial letters of the author first and last names. If a project were to be authored by Nick Leblanc, the project would be read as NLMyProject.

Examples:

Where does that come from? Did one person wrote it this way first and then everyone else followed?

I couldn't find anything about it, even in the Apple Guidelines. Is that idiom written anywhere?

  • 1
    Nice question, and cool open source examples! ;-) – Leo Natan Oct 15 '14 at 21:17
16

Objective C, like C, has no namespaces. This means that if someone has already defined a function foo or a class Bar, it must be globally unique and you can't define it. This can result in a lot of headaches when you have your code and then add another library and suddenly things break in strange places.

There is a guideline presented by Apple for this in Programming with Objective C: Conventions section of the iOS developer library:

Some Names Must Be Unique Across Your App

Each time you create a new type, symbol, or identifier, you should first consider the scope in which the name must be unique. Sometimes this scope might be the entire application, including its linked frameworks; sometimes the scope is limited just to an enclosing class or even just a block of code.

Class Names Must Be Unique Across an Entire App

Objective-C classes must be named uniquely not only within the code that you’re writing in a project, but also across any frameworks or bundles you might be including. As an example, you should avoid using generic class names like ViewController or TextParser because it’s possible a framework you include in your app may fail to follow conventions and create classes with the same names.

In order to keep class names unique, the convention is to use prefixes on all classes. You’ll have noticed that Cocoa and Cocoa Touch class names typically start either with NS or UI. Two-letter prefixes like these are reserved by Apple for use in framework classes. As you learn more about Cocoa and Cocoa Touch, you’ll encounter a variety of other prefixes that relate to specific frameworks:

...

Your own classes should use three letter prefixes. These might relate to a combination of your company name and your app name, or even a specific component within your app. As an example, if your company were called Whispering Oak, and you were developing a game called Zebra Surprise, you might choose WZS or WOZ as your class prefix.

So, they libraries that you mention kind of break this convention, but get at the heart of the problem of the lack of namespacing within Objective C.

Further Reading
Namespacing by NSHipster
What is the best way to solve an Objective-C namespace collision?
How does one use namespaces in iOS objective-c code?

  • Sounds like Objective-C really needs namespaces... – Chris Cirefice Oct 12 '14 at 19:33
  • 1
    @ChrisCirefice it would be really nice, but the thing to realize is that Objective C is a thin layer on top of C. Trying to introduce name spaces there is likely harder than it looks without breaking things. Digging in Why doesn’t Objective-C have namespaces? I found Clang Developers - Adding namespaces to Objective-C. Remember, @ is used because it isn't used in C... trying to add another operator that doesn't break other things is challenging. – user40980 Oct 12 '14 at 19:47
  • 1
    @ChrisCirefice Namespaces don't solve anything. Prefix clashes just become namespace clashes. – Miles Rout Oct 15 '14 at 5:49
  • @MichaelT Objective C has not been a thin layer on top of C for a while (Objective C 2.0). Now Apple has shifted focus to Swift, which includes more modern features, such as namespaces, but currently lacks other features of Objective C. – Leo Natan Oct 15 '14 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.