Diving into Objective-C

Published on January 15, 2011 by Amir Sedighi

Introduction
After many years of development I just wondered at the time I got familiar with Objective C. I’ve had long experience with Java, Delphi and C#. Also I was familiar with modern DSLs such as Groovy. However, Objective C looks pretty different when you start to learn it. After two weeks of challenges I just abstracted this article that consists a fast booting steps for everyone who just starts to learn Objective C.

Develop Interface First
In java developing an interface is recommended but it is not a necessary step to develop a class. However in Objective C you always have to start with the interface.
Interface file has a “.h” extension such as Contact.h that you can see it’s content below:

@interface Contact : NSObject {
	NSString *name;
	NSNumber *age;
}

@property(readwrite, retain) NSString* name;
@property(readwrite, retain) NSNumber* age;

@end

Interface starts with @interface annotation. Contact is the name of the class which we want to implement. NSObject is just like Object in Java or TObject in Delphi. I mean it is the most primitive object that you can use to develop your customized entities based on it.
NSString is just String. Unfortunately Apple didn’t choose better names for it’s primitive datatypes during years. “NS” comes from NeXTStep that is an old project that managed by Steve Jobs long time ago. The star before name variable is just the pointer sign. Don’t worry about it. It doesn’t make any problem.
A Property declares with @property annotation. Check this out to know what does it mean.
“readwrite” Indicates that the property should be treated as read/write. This is the default.

Implementation Next

This is the time to develop the implementation of the interface. Implementation always has a “.m” extension such as following:

#import "Contact.h"

@implementation Contact
@synthesize name, age;

@end

The implementation part starts with importing the interface first. Then it declares that is an implementation of the Contact interface. The most interesting definition that I’ve found in Objective C is @synthesize annotation. Here both a getter and setter method of properties will be required in the @implementation. If you use @synthesize in the implementation block, the getter and setter methods are synthesized automatically.

Main Program

Here is the main program. NSAutoreleasePool is the most convenient way for automatically releasing Objective C objects. I believe Java and C# object’s releasing mechanism is more advanced over Objective C. In Objective C you can release objects by releasing them one by one or adding them into a autorelease pool. I prefer second way. Below is a main() method sample that uses Contact.h. Here we defined and initiated an autorelease pool (NSAutoreleasePool). Then we defined an instance of Contact class that is also autorelease. It is not such a Java or C# self describing and clear code. But there is no other way to develop on iOS.

#import
#import "Contact.h"

int main (int argc, const char * argv[]) {

	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Contact *myFriend = [[Contact alloc] autorelease];

    NSNumber *newAge = [NSNumber numberWithInt:30];

    [myCar setAge:newAge];
    [myCar setName:@"Sara"];

    NSLog(@"The contact is: %@", [myFriend name]);
    NSLog(@"The his/her age is: %@", [myFriend age]);

    [myFriend release];

	[pool drain];

	return 0;
}

Objective C has a different syntax and conventions in compare with other popular programming languages. It is not as much easy as Java in my experience. But practice makes perfect. So just do it.

Posted in Cocoa/Objective-C

Leave a Reply

Categories

Archives