I've been learning Objective-C from Stephen G. Kochan "Programming in Objective-C (1st Edition, which has some challenges on Mac OS 10.6).
For a programmer familiar with (a) C programming and (b) Object Oriented Programming (OOP), Part I of the book (which covers Objective-C programming without the Foundation Libraries) is a fairly quick read. The key chapters with new information are:
Chapter 2: first program and compiling (see notes about Mac OS X 10.6)
Chapter 3: syntax of classes objects
Chapter 7: separation of interface and implementation
Chapter 8: inheritance
Chapter 9: polymorphism, dynamic typing and introspection features
Chapter 11: categories, posing and protocols
In summary Objective-C is:
Object Oriented Programming with Single Inheritance and Protocols (list of methods that are guaranteed to be implemented -- like Java interfaces; Objective-C uses
@interface
for something else)Unique syntax (see note above about compiling):
#include @interface Coord : Object { int x; int y; } -(void) setX: (int) nx andY: (int) ny; @end @implementation Coord -(void) setX: (int) nx andY: (int) ny { x = nx; y = ny; } @end int main(int argc, const char* argv[]) { Coord *myCoord = [Coord new]; [myCoord setX: 5 andY: 10]; [myCoord free]; }
Note that ordering of arguments to methods is fixed, despite the usual convention of naming additional arguments (cf Python)
Uses
id
for a generic object pointer (similar tovoid
in C but points to a base structure of the object), andself
for the current object (ie,this
in C++), andsuper
to run parent methods (like Java)Uses "-" as a prefix for methods which take an object parameter, and "+" for methods that don't (cf
static
methods in Java)Generally uses
#import <HEADER>
to bring in header files, which is like#include <HEADER>
but guarantees single inclusionSupports sending messages (ie, invoking methods) on
nil
(silently does nothing) which reduces need to check for, eg, allocation failure; thealloc
method also zero initialises all object data to aid benefit from this.Accessor methods are in a separate namespace from variables, so can have the same name; and there is some syntactic sugar which translates
object.method
into[object method]
(without any parameters, for accessor methods taking no parameters and returning a value which is used by the accessing code. (Note this works only if the accessor method can be verified at compile time, so doesn't work for things inid
variables.)Introspection with
isKindOf
(isKindOfClass
),isMemberOf
(isMemberOfClass
), etc (outside parenthesis in Object; inside paranthesis are equivilients in NSObject). Can also check if it has a particular method implemented.Categories allows addition of methods for an object without necessarily having the source to the Object available:
#import @interface Class (Category) -(void) newMethod: (int) a; @end
Posing allows substitution of your (extended) implementation of a class for the default one (seems to be deprecated as of MacOS 10.5)
Protocols specify interface but not implementation:
@protocol NSCopying -(id) copyWithZone: (NSZone *)zone; @end @interface AddressBook: NSObbject <NSCopying> // ... @end //... id <NSCopying> obj;
Suffers from Java's (at least historical) problem of collection classes being unable to deal with native types, see eg, NSNumber for Objective-C Foundation Library's workaround. (Cf C++ Templates which for all their complexity can work directly with native types.)
Creates constant string objects with
@"string text"
(note '@' at the start); literal strings are C strings. These string objects appear to be interchangable with other string objects in the Foundation Library. NSLog can be used to print these strings out, taking a printf() like set of parameters;%@
can be used to reference another string object (more generally%@
invokes thedescription
property on the object to get a printable string, so can be used on other objects too -- the default is the name of the class and memory address of the object, useful for debugging).The default string objects are immutable, as is NSString; there is also an mutable variant NSMutableString.
Foundation Libraries
The Foundation Libraries were invented by NextStep, and adopted by Mac
OS X (which is based on NextStep) and are recommended for any
Objective-C development (AFAICT virtually no one does any programming
in Objective-C without using the Foundation Libraries at minimum --
hence the confusion created by the 1st Edition of the book I'm using
with 64-bit programs). Amongst other things this means that objects
should descend from NSObject
rather than
Object
, which implies some different methods. It also means
slightly different memory handling, involving a memory allocation pool.
Apple's Documentation for Foundation Library is the best online reference. (In theory this should also be installed as part of the SDK, but I've not yet found the documentation on my computer.) More generally the Apple Cocoa Fundamentals Guide and Apple's Objective-C Programming Guide also contain lots of useful information. (In particular the Objective-C programming guide is filled with side notes that describe the implementation of the Objective-C features as extensions of C.)
One other useful online resource:
Programming Mac OS X with Cocoa for Beginners (on Wikibooks), which is based on Mac OS 10.3 with some notes about 10.4 features. (Quite a few things were deprecated with the release of Mac OS X 10.5, so cross referencing with the Apple documentation is probably essential.)