Anyone can explain me what this line mean ? I use to see (nonatomic,retain) it's first time I see the "assign" keyword in:
@property (nonatomic, assign) id <IconDownloaderDelegate> delegate;
Thx for your help,
Stephane
Building on Peter's answer:
When you create a property, you can automatically create getter and setter methods with the @synthesize
directive. The compiler not only creates two methods
- (id) delegate;
- (void) setDelegate: (id) newDelegate;
but also puts extra code around this to prevent multiple threads from changing the property at the same time (essentially a lock). nonatomic
tells the compiler that the code does not need to be thread safe, which means less code and better performance.
A setter created by the compiler with retain
would look something like this:
- (void) setDelegate: (id) newDelegate {
if (delegate != newDelegate) {
[delegate release];
delegate = [newDelegate retain];
}
}
and is why you need to release retained properties in the dealloc
method of your class.
Since the general advice is to not retain your delegate, you use assign
instead of retain
and the setter would look like this:
- (void) setDelegate: (id) newDelegate {
if (delegate != newDelegate) {
delegate = newDelegate;
}
}
First i thought to explain you here but i found this link and think might clear your doubt in well.
Specifies that the setter uses simple assignment. This attribute is the default.
You typically use this attribute for scalar types such as NSInteger and CGRect, or (in a reference-counted environment) for objects you don’t own, such as delegates.
retain and assign are effectively the same in a garbage-collected environment.
Assign is simply an assignment like int x = y with no memory management (like with retain).