Quantcast
Channel: NSArray of weak references (__unsafe_unretained) to objects under ARC - Stack Overflow
Viewing all articles
Browse latest Browse all 13

Answer by Harish Kumar Kailas for NSArray of weak references (__unsafe_unretained) to objects under ARC

$
0
0

To add weak self reference to NSMutableArray, create a custom class with a weak property as given below.

NSMutableArray *array = [NSMutableArray new];Step 1: create a custom class @interface DelegateRef : NSObject@property(nonatomic, weak)id delegateWeakReference;@endStep 2: create a method to add self as weak reference to NSMutableArray. But here we add the DelegateRef object-(void)addWeakRef:(id)ref{  DelegateRef *delRef = [DelegateRef new];  [delRef setDelegateWeakReference:ref]   [array addObject:delRef];}

Step 3: later on, if the property delegateWeakReference == nil, the object can be removed from the array

The property will be nil, and the references will be deallocated at proper time independent of this array references


Viewing all articles
Browse latest Browse all 13

Trending Articles