Problem with NSFastEnumeration and ARC protocol
I am having a problem implementing the NSFastEnumeration protocol while porting Objective-C code to ARC. Can anyone tell me how to get rid of the following warning (see code snippet)? Thanks in advance.
// I changed it due to ARC, was before
// - (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*) state objects: (id*) stackbuf count: (NSUInteger) len
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*) state objects: (__unsafe_unretained id *) stackbuf count: (NSUInteger) len
{
...
*stackbuf = [[ZBarSymbol alloc] initWithSymbol: sym]; //Warning: Assigning retained object to unsafe_unretained variable; object will be released after assignment
...
}
- (id) initWithSymbol: (const zbar_symbol_t*) sym
{
if(self = [super init]) {
...
}
return(self);
}
source to share
With ARC, something has to contain a strong or auto-implementing reference to every object, otherwise it will be released (as the warning warns). Since stackbuf
- __unsafe_unretained
, it won't hang on ZBarSymbol
for you.
If you create a temporary autorun variable and seal your object there, it will live until the current autorun pool appears. Then you can point stackbuf
to it without complaint.
ZBarSymbol * __autoreleasing tmp = [[ZBarSymbol alloc] initWithSymbol: sym];
*stackbuf = tmp;
source to share