Clicky

iOS Dev Nugget 5 Always use a prefix for category methods and category names

.

Need to run a code review on your codebase? Hire me

Since Objective C doesn't support namespaces, it is good to use prefixes for category methods like this:

@interface UIViewController (MOUIViewControllerAdditions)

- (void)moSomeFunction1();
- (void)moSomeFunction2();

@end

Notice that the category name has the same prefix (“mo”). It is especially important to give a prefix to your category methods because good function names can easily clash with an existing method of the same class either in the existing SDK or in a future version and be hard to detect.

It's a good habit to do the same thing for class names if you want to package them into libraries:

@interface MOMyClass : NSObject

@end

Use any prefix — one that is an abbreviation of your name or your company's name is good — as long as it isn't used by Apple or in popular open source libraries, e.g. stay away from the NS and UI prefixes as they are used by Apple.


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues