Sunday, 4 January 2009

Some comments on our first Objective-C program

In the first little program I use what is called instance methods and class methods.  The definition of these methods can be found in the .h files (interface), after the brackets ({}).
For example in the class Human you find a method - (NSString  *) name

The method declaration starts with a - (minus), this indicates an instance method. A method that starts with a + (plus) is a class method.
The typical usage of a class method is for instantiations of the class (new).

If you look again at above method, you must interpet this as a method name that returns a (NSString *).

If a method returns an id , then that means that it returns a generic object (= like a pointer to an object).

Another important language construct is the way to send messages, this is done using the square brackets syntax :

[self privateNew]

Above line means that the message privateNew is send to self.  (self = the current object or class).

Note that I use classes like NSString, NSException, NSObject etc. These classes are not part of Objective-C but are part of the so-called Foundation framework.
Apple delivers a lot of frameworks with XCode (like Cocoa, Carbon, OpenGL, Quartz, etc ).

The NSObject for example provides you with a number of methods you can use to instantiate objects, initialize them etc.

For example in the class Human for example I've overridden the new method so that nobody can make instances of the class Human.

No comments:

Post a Comment