Objective-c 2.0 features

Synthesize take place by default:-
In previous version , if we declare properties then we have to synthesize that property. But in modern
objective-c, we don’t require to synthesize the proeprty.
In modern objective-c,

@Interface MyClass : NSObject

@property (nonatomic, retain) NSString* myProperty;


@Implementation MyClass

//Don’t require to synthesize the property in modern objective-c. Compiler automatically
synthesize it.

Method ordering:-
In previous version, if method call before defining it and without declaring it, then it will give compile
time error. But in modern version if method invoke before definition and without any declaration ,then
complier will give no error.

NSNumber literals:-
In previous version, the initialization of NSNumber objects require class method.

Eg:- NSNumber* number =  [NSNumber numberWithInt:12];

But in modern objective-c  , NSNumber  objects can initialize like this:-
NSNumber* number  = @3;
For character,
NSNumber * number  = @’A’

Array Literals:-
In modern objective- c initialization of array literals   have less typing.

Eg:- As per previous version:-
NSArray* array = [[NSArray alloc] initWithObjects:@”A”, @”B” , @”C”, nil];
As per  modern objective-c:-
NSArray* array = @[@”A”, @”B”, @”C”, @”D”, @”E”];

Accessing array elements:-

As per previous version:-
NSString*  element = [array objectAtIndex:0];

As per modern objective-c:-
NSString* element = array[0];

The new syntax using index subscripting.

Creating mutable array as per new syntax:-
NSMutableArray* mutableArray = [@[@”A”, @”B”] mutableCopy];

Dictionary Literals:-
Initialize   dictionary as per previous version:-

NSDictionary* dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:object1, key1, object2,     key2, nil];

Initialize dictionary as per modern objective-c:-

NSDictionary* dictionary =  @{Key1: object1, key2:object2};

As with array, subscripting can also use in referencing dictionary items in modern objective-c.

object1  = dictionary[Key1];

Its a return type keyword thats return an instance of a class they are called on.
In modern objective-c use instancetype instead of id where appropriate. It provide more type safety
instead of id.
Suppose there is a class

@Interface MyClass: NSObject

+(instancetype) methodA;
+(id) methodB;


@Implementation MyClass

+(instancetype) methodA
return [[[self class] alloc] init];

return [[[self class] alloc] init];


The methodA return an instance of kind MyClass and methodB return an instacne of id.
So suppose if we write any method and wrote some code like:-

[[MyClass methodA] count];


[[MyClass methodB] count];

then first line of code will give compile time error becuase no count method is declare and define in MyClass, but second line return id type object and compile will not give any warning in this case.

Optional garbage collector and fast enumeration:-

Objective-c 2.0 have optional garbage collector option and also have fast enumeration  feature.

Written By: Yogesh Arora, Software Engineer, Mindfire Solutions


About yogesha2014

Currently, i am working in MindfireSolution as cocoa application developer. I have 2+ year of experience in mac desktop application development. Also have knowledge about VC++ win32, MFC and MonoDevelop GTK#2.0.

Posted on September 29, 2014, in Objective-C and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: