Dynamic and static typingDynamically-typed objectStatically-typed object
The null object pointer Test for nil explicitlyif (person == nil) return; Or implicitlyif (!person) return; Can use in assignments and as arguments if expected Sending a message to nil?
BOOL typedefWhen ObjC was developed, C had no booleantype (C99 introduced one)ObjC uses a typedef to define BOOL as a typeMacros included for initialization and comparison:YES and NO
Selectors identify methods by name A selector has type SELSEL action = [button action]; ![buttonsetAction:@selector(start:)]; Conceptually similar to function pointer Selectors include the name and all colons, for example:-(void)setName:(NSString *)name age:(int)age;would have a selector:SEL sel = @selector(setName:age:);
DemoUsing selector and respondsToSelector: method
Class IntrospectionYou can ask an object about its classTesting for general class membership(subclasses included):Testing for specific class membership(subclasses excluded):
Object Identity versus EqualityIdentity—testing equality of the pointer valuesEquality—testing object attributes
Object Creation Two step process allocate memory to store the object initialize object state + allocClass method that knows how much memory is needed - initInstance method to set initial values, perform other setup Create = Allocate + Initialize
Implementing your own - init methodDemoMultiple init methods
Memory Management Allocation DestructionC malloc freeObjective-C alloc deallocCalls must be balanced Otherwise your program may leak or crash
Reference CountingEvery object has a retain count Defined on NSObject As long as retain count is > 0, object is alive and valid+alloc and -copy create objects with retain count == 1-retain increments retain count-release decrements retain countWhen retain count reaches 0, object is destroyed -dealloc method invoked automatically One-way street, once you’re in -dealloc there’s no turning back
Reference counting in actionPerson *person = [[Person alloc] init]; Retain count begins at 1 with +alloc[person retain]; Retain count increases to 2 with -retain[person release]; Retain count decreases to 1 with - release[person release]; Retain count decreases to 0, -dealloc automatically called
Object Lifecycle RecapObjects begin with a retain count of 1Increase and decrease with -retain and -releaseWhen retain count reaches 0, object deallocatedautomaticallyYou never call dealloc explicitly in your code Exception is calling -[super dealloc] You only deal with alloc, copy, retain, release
AutoreleaseCalling -autorelease flags an object to be sentrelease at some point in the futureLet’s you fulfill your retain/release obligationswhile allowing an object some additional time toliveMakes it much more convenient to managememoryVery useful in methods which return a newlycreated object
Method Names & AutoreleaseMethods whose names includes alloc, copy, ornew return a retained object that the callerneeds to releaseAll other methods return autoreleased objectsThis is a convention- follow it in methods youdefine!
How does -autorelease work?Object is added to current autorelease poolAutorelease pools track objects scheduled to bereleased When the pool itself is released, it sends -release to all its objectsUIKit automatically wraps a pool around everyevent dispatch
Hanging Onto an Autoreleased ObjectMany methods return autoreleased objectsIf you need to hold onto those objects you needto retain them
DemoReturn auto released object and retain it.