Objective-C Style Guide

In the past releases of Xcode, the current version is now Xcode 4.5, Apple made some subtle changes to Objective-C. If you google for some tutorials, you'll see old code all over the place. The purpose of this article is to review some of the changes over the years in Objective-C. Some of this changes will make your code look cleaner and thus simpler. As an example, look at the following block of code. We'll change this code along the way, as we review the changes in Objective.c The header file will look like this.

//ViewController.h

@interface ViewController: UIViewController
{
// instance vars for outlets
IBOutlet UIButton *myButton;
IBOutlet UILAbel *myLabel;

// private instance vars
NSString *myname;
NSMutableArray *myArray;
}

// IBOutlet properties
@property (nonatomic, strong) IBOutlet UIButton *myButton;
@property (nonatomic, strong) IBOutlet UILabel *myLabel;

// IBAction Methods
- (IBAction)buttonpressed:(id)sender;

// private methods
- (void)fillArrayWithValues:(NSMutableArray *)myArray;

@end

 

And the implementation file looks like this

 

//ViewController.m

#import "ViewController.h"

@implementation ViewController

@synthesize myButton;
@synthesize myLabel;

// method implementations
...

@end

A lot of programmers still define the properties and include the backing instance variables in the @interface section. Essentially, this is writing the same thing twice. When you synthesize a property, the compiler automatically creates the instance variable. So you can remove these instance variables from the header file.

IBOutlet UIButton *myButton; 
IBOutlet UILAbel *myLabel;

As for the private instance variables , these are accessed by this viewcontroller only, so these should not be accessed by external objects. Move these private instance variables to the implementation file instead.

@implementation ViewController 
{
NSString *myname;
NSMutableArray *myArray;
}

In Xcode 4.5, the synthesize statements are not needed anymore, the compiler will detect a @property statement and automatically will synthesize the property. Delete the synthesize statements. This feature is backwards compatible back to iOS 4.

@synthesize myButton; 
@synthesize myLabel;

Objective-C 2.0 has a mechanism allowing to add private methods and private variables to a class. The feature is called Class Extensions. Insert the class extension right after the #import statement in ViewController.m

@interface ViewController (myClass) 

@end

Outlet properties are needed to connect items from the nib, so these will be used internally only. Move these outlet properties to the class extension.

@interface ViewController (myClass)

@property (nonatomic, strong) IBOutlet UIButton *myButton;
@property (nonatomic, strong) IBOutlet UILabel *myLabel;

@end

IBAction methods also are used internally only, so we move this method also to the class extension.

@interface ViewController (myClass)

@property (nonatomic, strong) IBOutlet UIButton *myButton;
@property (nonatomic, strong) IBOutlet UILabel *myLabel;

- (IBAction)buttonpressed:(id)sender;

@end

The private methods, also don't need to be visible by an external object. So we create a forward declaration in the class extension where you can declare these methods. This way, the compiler knows the existence of a method. Of course, the method declaration also can be removed from the header file.

@interface ViewController (myClass)

@property (nonatomic, strong) IBOutlet UIButton *myButton;
@property (nonatomic, strong) IBOutlet UILabel *myLabel;

- (IBAction)buttonpressed:(id)sender;
- (void)fillArrayWithValues:(NSMutableArray *)myArray;

@end

But it gets better, as of XCode 4.3 the compiler can figure out for himself the methods exists. So we can move the declarations completely.

- (void)fillArrayWithValues:(NSMutableArray *)myArray;
- (IBAction)buttonpressed:(id)sender;

There you have it the final code nowe looks like this.

 

//ViewController.h

@interface ViewController: UIViewController

@end

//ViewController.m

@interface ViewController (myClass)

@property (nonatomic, strong) IBOutlet UIButton *myButton;
@property (nonatomic, strong) IBOutlet UILabel *myLabel;

@end

@implementation ViewController
{
NSString *myname;
NSMutableArray *myArray;
}

// method implementations
...

@end

As you can see, the code is much cleaner and simpler this way.