Quantcast
Viewing all articles
Browse latest Browse all 13

Answer by Ciprian C for NSArray of weak references (__unsafe_unretained) to objects under ARC

If you use a lot this comportment it's indicated to your own NSMutableArray class (subclass of NSMutableArray) which doesn't increase the retain count.

You should have something like this:

-(void)addObject:(NSObject *)object {    [self.collection addObject:[NSValue valueWithNonretainedObject:object]];}-(NSObject*) getObject:(NSUInteger)index {    NSValue *value = [self.collection objectAtIndex:index];    if (value.nonretainedObjectValue != nil) {        return value.nonretainedObjectValue;    }    //it's nice to clean the array if the referenced object was deallocated    [self.collection removeObjectAtIndex:index];    return nil;}

Viewing all articles
Browse latest Browse all 13

Trending Articles