【发布时间】:2011-10-19 00:33:49
【问题描述】:
在更新下面的代码以使用 iOS 5 的自动引用计数时,在尝试执行快速枚举时为“state->itemPtr”分配缓冲区时发生错误,以便可以使用“迭代实现类” foreach”循环。我得到的错误是“将'__autoreleasing id *'分配给'__unsafe_unretained id*'会更改指针的保留/释放属性”。请参阅带有注释的代码行。
/*
* @see http://cocoawithlove.com/2008/05/implementing-countbyenumeratingwithstat.html
* @see http://www.mikeash.com/pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html
*/
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState *)state objects: (id *)buffer count: (NSUInteger)bufferSize {
NSUInteger arrayIndex = (NSUInteger)state->state;
NSUInteger arraySize = [_tuples count];
NSUInteger bufferIndex = 0;
while ((arrayIndex < arraySize) && (bufferIndex < bufferSize)) {
buffer[bufferIndex] = [_tuples objectAtIndex: arrayIndex];
arrayIndex++;
bufferIndex++;
}
state->state = (unsigned long)arrayIndex;
state->itemsPtr = buffer; // Assigning '__autoreleasing id *' to '__unsafe_unretained id*' changes retain/release properties of pointer
state->mutationsPtr = (unsigned long *)self;
return bufferIndex;
}
本例中的 _tuples 变量是 NSMutableArray 类型的实例变量。
如何解决此错误?
【问题讨论】:
标签: objective-c ios ios5 automatic-ref-counting