Whilst I am waiting for XCode to download on my work laptop I have been reading the objective C primer on the apple developer website.
The apple developer site is immense, with lots of detailed documentation and tutorials, very much like MSDN. I will try and sum up what I have learnt from this article a little more succinctly.
I will also be following the Stanford course on Iphone Application Programming
Objective C
Objective C is a superset of ANSI C adding object oriented elements amongst other things.
This is how you declare a class:

You declare this bit in a .h (header) file.
In general all classes you create will inherit from NSObject which allow them behave like Objective C objects at runtime.
All objects have an “isa pointer” which tells you what class the object is an instance of. The keyword “nil” is null.
Strong and weak typed pointers
AAARGH POINTERS, MERCY!. Dont be silly, pointers are our friends. When you store objects in variables you always use a pointer type. Strongly typed pointers include the class name in the variable type. Otherwise there are weakly typed which use “id” instead.
You use weakly typed pointers when you might not know the type of the object which allows for dynamic typing. Technically all objects are of type “id” which simply tells the runtime that something is an object. All objects are dynamically typed at runtime, whenever the system needs to know the type of an object, the object can let it know.
There is no garbage collection for objective C on the iPhone so the developer is responsible for this (uh oh).
Type introspection
To identify what type an object is you can call the method “isMemberofClass”.
if ( [anObject isMemberOfClass:someClass] )
To see if an object inherits from or is a member of a particular class you have to call “isKindOfClass”
Instance and Class methods
Classes can have two kinds of methods declared:
- Instance: Can only be invoked by an instance of a class - dennoted by a minus sign
- Class: Do not need an instance, so a static method if you’re from the land of C# - dennoted by a plus sign
Apple likes to name calling a method “messaging”. As far as I can tell its the same as calling a method, you put in the name of a method and give it the parameters it needs by using square brackets.
So to call a method of the following signature -
-(void)insertObject:(id)anObject at Index:(NSUInteger)index
You would write -
[myArray insertObject:anObject atIndex:0];
“Messages” can be nested.
Dot notation
Thankfully apple added the syntactic sugar of dot notation:
myInstance.value = 10;
Is the same as:
[myInstance setValue:10];
Implementing a class
To implement the class above, in a .m file you could do this:
@implementation MyClass- (id)initWithString:(NSString *)aName
{
if (self = [super init]) {
name = [aName copy];
}
return self;
}
+ (MyClass *)createMyClassWithString: (NSString *)aName
{
return [[[self alloc] initWithString:aName] autorelease];
}
@end
Declared Properties
Using the @property compiler directive you can create c# like properties for your classes so you dont have to write getter and setters.
@property (readwrite,assign) int Var;
test.Var = 10;
You can use “readonly” with rather obvious consequences. There are many other attributes you can use with the @property directive which you can read about here. If you just use @property without any attributes you will have a “setVar” method made for you by the compiler.
Creating instances of classes
To create the memory required for your object and get a pointer to it, call the alloc method of NSObject class
Rectangle *myRectangle;
myRectangle = [Rectangle alloc];
However, the convention is to declare an “init” method on your classes to get it into a safe state, so the usual syntax for making instances is:
id myRectangle
myRectangle = [[Rectangle alloc] init];
Just to clear up, you will notice i ommitted the * from the second example because i was using an “id” type, which is a pointer type. For that reason you should make sure that your init method declaration return type is id. Doing return self; will achieve this.
Strings
Thankfully in objective C there is a nice NSString class which you should always use when making strings. They provide unicode support and can be dynamic in length. There is a shorthand for creating strings by doing a double quote preceeded by @.
NSString *myString = @"Hello world \n";



