Objective-C for Java (and Ruby) Developers – Part 1

25 Feb 2010 – Denver, CO

Brought to you this morning by my current favorite method of caffeination:


Dunkin Donuts

Classes, Variables & Methods

Classes are divided into 2 parts:

 
	@interface // definitions go here
	
	...
	
	@implementation // goes here

Methods are defined using the syntax:

 
	-(void) add: (int) a and: (int) b;

With the – at the start meaning an instance-level variable, and a + meaning a class level definition. Giving variables names in each method definition can feel a little strange. Heres a couple more examples:

 
	-(void) setNumerator: (int) n overDenominator: (int) d;
	-(void) addEntryWithName: (*NSString) name andEmail: (*NSString) email andPhone: (*NSString) phone;

The names can also be ignored if you wish.

 
	-(void) add: (int) a: (int) b;

Methods are invoked via the syntax:

 
	[receiver message];

Variables can be accessed using the ‘dot operator’:

 
	myInstance.myVar;

And can be quickly defined using the @synthesize operator, which is somewhat equivalent to Rubys attr_accessible:

 
	@synthesis myVar;

To bring it all together:

 
	#import <Foundation/Foundation.h>

	@interface Fraction : NSObject 
	{
		@public
			int public_variable;
	}

	@property int numerator, denominator;

	-(void) setNumerator: (int) n andDenominator: (int) d;
	-(void) setA: (int) a;

	@end

	@implementation Fraction

	@synthesize numerator, denominator;

	-(void) setNumerator: (int) n andDenominator: (int) d
	{
		numerator = n;
		denominator = d;
	}

	-(void) setA: (int) a
	{
		public_variable = a;
	}

	@end


	int main (int argc, const char * argv[]) {
	    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

		Fraction *f = [[Fraction alloc] init];
		[f setNumerator: 1 andDenominator: 2];
		NSLog(@"numerator: %i, denominator: %i", [f numerator], [f denominator]);

		[f setA: 3];
		NSLog(@"via attribute accessor: %i", f->public_variable);

	    [pool drain];
	    return 0;
	}

The ‘id’ Data Type and Static Typing

In Java if we didn’t feel like being explicit about a type, we could always just define a super-class as the reference, for example:

 
	public class Philly
	{
		public static void main(String[] args) throws InterruptedException
		{
			Object p = new Philly();
			System.out.println(p.getClass());
		}
	}

Of course gives us the output Philly.

In Objective-C we get this weird little ‘id data type’ that can basically do the same thing. My, you are a weird little man, aren’t you ?

 
id g = [[Fraction alloc] init];
NSLog(@"numerator: %i, denominator: %i", [g numerator], [g denominator]);

Note that we didn’t even have to declare g as a pointer there.

Up next: Meta-data, Categories & Protocols.