// *** before: ***
@interface Cell : NSObject
@property (nonatomic, retain) NSString *title; // holds it
@property (nonatomic, assign) id delegate; // does not hold it
@end
// assign was dangerous: after the delegate was deallocated the field kept
// pointing at freed memory, and the next message crashed the program
Controller *controller = [[Controller alloc] init];
cell.delegate = controller;
[controller release];
[cell.delegate reload]; // EXC_BAD_ACCESS on a dangling pointer
// *** in version 2011: ***
@interface Cell : NSObject
@property (nonatomic, strong) NSString *title; // holds it, retain renamed
@property (nonatomic, weak) id delegate; // does not hold, and is
// set to nil automatically
@property (nonatomic, unsafe_unretained) id owner; // old assign, kept for
// classes that forbid weak
@property (nonatomic, copy) NSString *name; // copy is unchanged
@end
Controller *controller = [[Controller alloc] init];
cell.delegate = controller;
controller = nil; // the last strong reference is gone
[cell.delegate reload]; // delegate is already nil - a message to nil
// does nothing instead of crashing
// strong is the default for an object property, so it may be omitted;
// weak is the answer to a retain cycle: parent -> strong -> child,
// child -> weak -> parent