o9s
is a microblog
home archives search feed blogroll


An Opiniated Objective-C Style Guide

Basics

Use CamelCase for class names and constants, camelCase for methods and all caps for macros.

The star (*) for pointers should be next to the variable and not the class name (example: NSDictionary \*result).

Method signatures should have no extra spaces except after the -/+ (example: - (void)sort:(NSArray\*)arr;).

Instance variables should be prepended with an underscore.

Imports

Only use #import in .m files unless it’s an import for the base class or a delegate. If you have to use a class name in a method signature, use a forward class declaration (@class).

Sort your imports alphabetically, this helps to prevent duplicated imports. It makes it more obvious if there are any.

Typedefs

Use the name of the class followed by the suffix Block. Use a noun to describe the block.

For example, a class named TokenManager would could have a typedef named TokenManagerCompletionBlock, TokenManagerErrorBlock or TokenManagerSuccessBlock.

If many typedefs are used in the same class. Use the name of the class followed by the name of the method that uses it and suffixed by Block with an informative noun.

This naming convention helps to avoid conflicting typedef definitions.

Categories

Prefix all functions in categories with a unique name followed by an underscore to avoid conflicts following an update of the Apple libraries or a pod (example: - (NSString*)myUniqueName_toString;).

Posted on 2018-02-16   #objectivec  






← Next post    ·    Previous post →