Quicknotes on Objective-C and Cocoa:

self – refers to this object (similar to this in C++)
super – refers to super class object (no c++ equivalent)

Later you can do

if([someObj respondsToSelector:mySelector]) {
    [someObj performSelector:mySelector withObject:self];
}

Selectors are used for Target/Action in Cocoa.

Introspection:
Determine the class of an object at runtime.

if([myClass isKindOfClass:[NSControl class]]) {
    /*true if myClass is an object of type NSControl or of any class that
     * inherits from NSControl*/

}

if([myClass isMemberOfClass:[NSControl class]]) {
    /*true if myClass is an object of type NSControl*/
}

Identity vs Equality:

if (obj1 == obj2) {
    /*same object instance*/
}

if ([obj1 isEqual:obj2]) {
    /*logically equivalent*/
}

Protocols (Formal, informal):
Adding interfaces for others to implement
To declare an informal protocol -

@interface BaseObject (MyProtocol) : NSObject
method1();
method2();
@end

Any classes that implement this protocol have to declare the methods again in their own interface files and define them along with other methods in their implementation files.

Formal protocols –
Advantages over informal protocols – can check at runtime if an objects implements a protocol.

@protocol ProtocolName
//method declarations
@end

Implementing class:
@interface ClassName: MySuperClass <protocol1, protocol2>
@end

The implementing class must import the header  where the protocol is declared. In objective-C 2.0 classes implementing a protocol are required to implement all of the methods.

Check if an object conforms to a protocol:
if ([receiver conformsToProtocol:@protocol(MyProtocol)]) {
}

Categories: Adding methods to existing classes
Example:

#import “ClassName.h”

@interface ClassName (CategoryName)
//new method names
@end

@implementstion ClassName (CategotyName)
//method definitions
@end

Foundation classes:

NSEnumerator – used to iterate over collections (NSArray, NSSet, NSDictionary
etc). A little bit like STL iterator

NSEnumerator *en;
id object;

en = [aCollection objectEnumerator];

while((object = [e nextObject]) != nil) {
}

Misc useful functions:
All these functions return a NSString*

NSUserName() – logon name of current user
NSFullUserName() – full nam of current user
NSHomeDirectory – Home dir of current user
NSHomeDirectoryForUser(NSString *userName) – home dir of specified user
NSSearchPathForDirectoriesInDomains(…) – useful function, read the docs :-)

Memory management:

Objective-C uses reference counting. Every object you create holds a counter within itself that counts the number of pointers pointing to the object. This count can be increased by sending the object a retain message and decreased by sending it a release message. When an object finally receives a release message which decreases it’s count to 0 then it is deleted.

You are responsible for releasing objects that you create using any of the following class methods (or their variants) -

alloc, copy, new

These methods return objects with count set to 1 (so you have to [object release] when you are done with the object). If you want to hold on to data returned from other methods you have to send that object a “retain” message. Later when you are done with that object you sent it a release method.

When an object is added to an array, dictionary, set etc the collection retains it. It will be sent a corresponding release message when the collection itself is released.

Obj-C 2.0 (from Leopard onwards) introduces garbage collection. Not available on Tiger

Links:
Hold Me, Use Me, Free Me
Introduction to memory management programming guide for Cocoa;