I have a data structure that I wanted to list. I tried to implement my NSFastEnumerator object as follows:
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id [])buffer count:(NSUInteger)len { NSUInteger c = 0; while (c < len) { id obj = [self objectAtIndex:state->state]; if (obj == nil) break; buffer[c] = obj; c++; state->state++; } state->itemsPtr = buffer; state->mutationsPtr = nil; return c; }
If I use objectAtIndex directly, my object is working correctly. I get null when the index does not exist. But when I use the for loop:
for (Pin *pin in coll) { ... }
the code goes through the above function perfectly and fills the state with what appears to be valid values ββand returns the number of objects, then I get the EXC_BAD_ACCESS error in the for statement itself.
What am I doing wrong in this implementation?
source share