Whats the difference between this:
@property (nonatomic, weak) id <SubClassDelegate> delegate;
and this:
@property (nonatomic, assign) id <SubClassDelegate> delegate;
I want to use property for delegates.
The Swift book says that "to prevent strong reference cycles, delegates are declared as weak references."
Assign creates a reference from one object to another without increasing the source's retain count. Retain creates a reference from one object to another and increases the retain count of the source object.
The rule is to not retain it because it's already retained elsewhere and more important you'll avoid retain cycles.
The only difference between weak
and assign
is that if the object a weak
property points to is deallocated, then the value of the weak
pointer will be set to nil
, so that you never run the risk of accessing garbage. If you use assign
, that won't happen, so if the object gets deallocated from under you and you try to access it, you will access garbage.
For Objective-C objects, if you're in an environment where you can use weak
, then you should use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With