Introduction to Coding Guidelines for iPhone Apps

Introduction to Coding Guidelines for iPhone Apps

Coding standard can be a great benefit as it makes it easy to read any code, and is a good practices which leads to less defects.
Lots of articles are there on web over coding guidelines, Apple also provides coding standard -Coding Guidelines for Cocoa.
As writing a code according to coding guidelines is really very impressive and effective as well as very important.
Still many of us don’t follow any, here we have a very small and crisp idea of coding standard for iOS developers.


• Interface Files
• Implementation Files
• UIControls Prefixes List
• Before Deliver Source Code

Interface Files

The Interface Files/ .h files are header files. It is called header file because it only contains the ‘head’ or a class, in other words, all the properties and functions that any other outside class should know about, the ‘public face’ of a class. It does not contain implementation logics at all.

Standard to follow for .h file -
The interface files should be as small as possible and serve as a reference for what the class does and how to interact with it
• System imports go before project imports
• Don’t import anything that’s not needed for defining the interface
• Only expose the minimum necessary for other classes to interact. Everything else goes into implementation file
• Comment any non-obvious method

// MyClass.h

#import "MyOtherClass.h"

@interface MyClass : NSObject

@property (nonatomic, retain) MyOtherClass *blog;
@property (nonatomic, readonly, retain) NSArray *readonlyThing;
@property (nonatomic, assign) id delegate;

Explain in a few words about the methods
- (void)myClassMethods;

Implementation Files
Implementation Files/.m file contains all the implementation details of all the functions declared in .h and more. It also contains declaration and implementation of private functions that outside classes cannot use, or should not care about.

Standard to follow for .m file -

• If the interface file has declared readonly properties, redeclare them as readwrite to be able to use the accessors
• Declare any internal variable and methods in the implementation
• dealloc is the first method, followed by initialization methods (If not using ARC)
• Use “#pragma mark -” to group methods into sections

// MyClass.m

#import "MyClass.h"
#import "ClassDependencies.h"

@interface MyClass ()
@property (nonatomic, retain) NSArray *readonlyThing;
/** Private methods */
- (void)prepareStuff;

@implementation MyClass {
NSMutableArray *_things;
BOOL _flag;

@synthesize blog = _blog;
@synthesize delegate = _delegate;
@synthesize readonlyThing = _readonlyThing;

- (void)dealloc {
[super dealloc]; = nil;
self.readonlyThing = nil;
[_things release];
// ...

- (void)init {
self = [super init];
if (self) {
// Initialize
_things = [[NSMutableArray alloc] init];
return self;
#pragma mark - Public methods

- (void)myClassMethods {
[self prepareStuff];
// Do actual stuff

#pragma mark - Private methods

- (void)prepareStuff {
// Prepare stuff
// self.readonlyThing = ...


UIControls Prefixes List

Here goes some common prefixes for UIControls, that we should use as a prefix in declaration of UIControls Object, this make variables readable and easy to find out.

UIControl                              Prefix

UILabel                                    lbl
UIButton                                 btn
UISegmentControl                seg
UITextfield                             txtFld
UISlider                                  sldr
UISwitch                                  switch
UIActivityIndicatorView             ai
UIProgressView                     pv
UITableView                           tblView
UIImageView                         img
UITextView                            txtView
UIWebView                            wv
MKMapView                         mv
UIScrollView                          srlView
UIDatePicker                         dp
UIPickerView                         picker
UIView                                   view
UIWindow                             window
UINavigationBar                  navBar
UINavigationItem                navItem
UISearchBar                           sb
UIToolBar                              toolbar
UITabBar                               tabbar

Before Deliver Project
Before We deliver our project to client, we should take care of following things.

• Check for whitespace.
• No NSLog in commits.
• No commented out code.
• Don’t avoid compiler warnings.
• Analyze Source Code and handle memory leaks.
• Source Code should be proper documented.