My github example code


  • XCode 3.2.6
  • OSX 10.6.8)

Object-C knowledge


Categories are one of the most useful features of Objective-C. Essentially, a category allows you to add methods to an existing class without subclassing it or needing to know any of the details of how it's implemented.

#import <Cocoa/Cocoa.h>

@interface NSString (Utilities)
- (BOOL) isURL;
memory management

That's the theory of reference counting. But in practice, there are usually only two reasons to create an object:

  1. To keep it as an instance variable
  2. To use temporarily for single use inside a function

In most cases, the setter for an instance variable should just autorelease the old object, and retain the new one. You then just make sure to release it in dealloc as well.

So the only real work is managing local references inside a function. And there's only one rule: if you create an object with alloc or copy, send it a release or autorelease message at the end of the function. If you create an object any other way, do nothing.

Reference count with:

  • alloc (initial count to 1)
  • retain (increase 1)
  • release (decrease 1 call dealloc when count == 0)
  • copy (copy from source object and set count to 1 in new object which own by us now)
  • autorelease (is used when return newly created new object back to caller. Caller gets valid object and could retain if needed). The object will called release in the future time.
    • autorelease pool life cycle is from event start to handle to event end (start to wait event)
    • all object which set autorelease will be tracked in the pool
    • when the autorelease pool released, it will release all tracked object.

class method invocation, you throw a message to a object/class to invoke method you want.
message internal

  • [receiver message]
  • [receiver message:argument]
  • [receiver message:arg1 andArg:arg2]

<protocol> is just like Java's interface, it define behavior that cross classes.

Single inheritance

Only support single inheritance, others need to use <protocol> for behavior sharing between classes.


* Some kind of function pointer, it can use for callback
* selector usage
* selector late binding

class introspection

Some kinds of RTTI (Run-time type information)

if([myObject isKindOfClass:[UIControl class]) {...}
if([myObject isMemberOfClass:[NSString class]]) {...}


Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License