在将Objective-C代码迁移到ARC时,我在实现NSFastEnumeration协议时遇到了问题。谁能告诉我,如何摆脱下面的warnig (参见代码片段)?提前谢谢。
// 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);
}发布于 2013-01-25 03:18:30
使用ARC,某些东西必须持有对每个对象的强引用或自动释放引用,否则它将被释放(正如警告所说的那样)。因为stackbuf是__unsafe_unretained,所以它不会为您挂起ZBarSymbol。
如果你创建了一个临时的自动释放变量,并将你的对象存放在那里,它将一直存在,直到当前的自动释放池被弹出。然后,您可以将stackbuf指向它,而不会有任何抱怨。
ZBarSymbol * __autoreleasing tmp = [[ZBarSymbol alloc] initWithSymbol: sym];
*stackbuf = tmp;https://stackoverflow.com/questions/14502189
复制相似问题