Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use a prefix on Objective C classes?

According to Apple, two to three letter prefixes should be used

when naming classes, protocols, functions, constants, and typedef structures.

Does this include classes which are not intended to be part of a framework, simply used internally in an application? I realize this is relying on other developers that develop frameworks you might be using to use prefixes, but that seems acceptable. What about Core Data entities? If I generate classes from them, shouldn't they be prefixed as well?

like image 863
Don Avatar asked Dec 04 '09 06:12

Don


People also ask

What does NS mean in Objective-C?

The Foundation Kit, or just Foundation for short, is an Objective-C framework in the OpenStep specification. It provides basic classes such as wrapper classes and data structure classes. This framework uses the prefix NS (for NeXTSTEP). It is also part of Cocoa and of the Swift standard library.

What does NS stand for Apple?

It stands for NextSTEP, the operating system that Steve Jobs (caused to be) built after he was evicted from Apple. Objective-C and the NextSTEP API are the basis of the newer OpenSTEP and MacOS-X APIs.

Can you write C in Objective-C?

You really can't use C in Objective-C, since Objective-C is C. The term is usually applied when you write code that uses C structures and calls C functions directly, instead of using Objective-C objects and messages.

How does Objective-C categories work?

Categories provide the ability to add functionality to an object without subclassing or changing the actual object. A handy tool, they are often used to add methods to existing classes, such as NSString or your own custom objects.


2 Answers

To be safe, use a prefix for all classes in your application. All classes are loaded into a single flat namespace in Objective-C and the prefix prevents collisions both now and in the future.

This also includes CoreData entities; they should also use the same prefix as the rest of your application's classes.

like image 151
bbum Avatar answered Oct 07 '22 11:10

bbum


Also note that you may decide some of this code could be used elsewhere, so using prefixes now will safeguard you from potential collisions in the future.

like image 23
Daniel Tull Avatar answered Oct 07 '22 11:10

Daniel Tull