Question

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?

Était-ce utile?

La solution

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?

Licencié sous: CC-BY-SA avec attribution
scroll top