I have a NSArray containing objects. I want to create a secondary NSArray containing just some of the objects in the first NSArray. I have something like:
[array1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (something) {
[array2 addObject:obj]; // (1)
}
}];
[array1 release]; // (2)
I'm thinking that (1) will increase the object's retainCount (which will bring it from 1 to 2), then (2) will decrease it (which will bring it from 2 to 1 for the objects added in array2 and from 1 to 0 for the objects that did not get added).
So I expect that, after doing this, it will be safe to access the objects from array2, and the non-conformant objects that did not pass the test will be deallocated.
Is this correct?
NSMutableArrayfor the second arrayarray1against a predicate. See+[NSPredicate predicateWithBlock:]and-[NSArray filteredArrayUsingPredicate:]. An added benefit of this clearer approach is that you don't have to think so hard about memory management! :)