1

Let's say I have a class called colorPicker which holds a delegate to notify about color changes. Does the property for this delegate need to be set to retain or assign? WHY?

@interface ColorPicker : UIView

@property (nonatomic, retain) NSObject <ColorPickerDelegate> *delegate; 

@end
aryaxt
  • 76,198
  • 92
  • 293
  • 442

1 Answers1

4

Short: use assign to avoid retain cycles.

Retaining an object creates a strong reference, and an object cannot be deallocated until all of its strong references are released. If two objects retain each other, neither object ever gets deallocated because the connection between them cannot be broken (source)

Long: Why are Objective-C delegates usually given the property assign instead of retain?

Community
  • 1
  • 1
Regexident
  • 29,441
  • 10
  • 93
  • 100