This is the style guide that I follow when programming in Objective-C, this is for my own reference, but other people may find this useful.
General
- Use modern objective-c 2.0 syntax
- Use ARC whenever possible.
- Manual memory management should only be used when using older libraries that haven’t been updated yet.
- Use tabs when indenting, not spaces.
Code Naming Basics
- Follow the Apple naming guidelines
- Use descriptive names, even if they are long. Xcode has great code completion features. Use them.
- Clarity is preferred over all else.
- E.g. Compare the two methods:
[spaceship fly: 10.0f]and[spaceship flyAtSpeed: 10.0f]. - Note how much clearer the second declaration is, the method describes its intentions and parameter names.
- Don’t use abbreviations. E.g.
bgColorvs.backgroundColor - Custom resuseable classes should be prefixed with
AFG- Application level classes do not have to be prefixed. E.g. ViewControllers
- All view controllers should be descriptively named and end with
ViewController.- E.g.
CalculatorSettingsViewController
- E.g.
Commenting
- Follow the appledoc code formatting style.
- Only comment the method declarations. E.g. In the
@interfaceor private@interface ... ()code blocks. - Describe what the method does, the parameter names and the return value and what it signifies.
The following is a good example of method commenting:
/** This method asks the delegate if the AFGContainerViewController should pop the current view from the view stack.
@param - The current AFGContainerViewController object
@return A BOOL value indicating if the current view controller should be removed from the AFGContainerViewController's view stack.
*/
-(BOOL) AFGContainerViewControllerShouldPopContainerViewController: (AFGContainerViewController*) containerViewController;
Spacing
-(void) performOperationWithObject: (NSObject*) objectName {
NSLog(@"This is a well spaced method");
[objectName performSomeOperation];
}
- No space between the class or instance specifier
(+,-) - Single space between the return type and method name
- Single spaces between method signature, object types and object names
- Brace on the same line as the method signature.
- Optional newline below the method signature for readability
- No newline before the closing brace
Variables and Initializers
- Variable names should always start with lower case.
- Good:
numberOfObjects - Bad:
NumberOfObjects
- Good:
- Clearly identify the designated initializer for the class
- All other initializers should propagate up to and use the designated initializer.
- Override the superclass’ designated initializer.
- Initialization method should be at the top of the
@implementation - Don’t explicitly initialize variables to nil. Under ARC this happens automatically.
Properties
- Don’t
@synthesizeproperties. Modern objective-c does this for us automatically. - Always start with a lower case letter.
NSStringproperties should always becopyIBOutletsshould always beweak
Other readings
These are some good references / starting points for creating your own style guide or just to see other coding styles.