自动引用计数:快速枚举时出错 [英] Automatic Reference Counting: Error with fast enumeration

查看:217
本文介绍了自动引用计数:快速枚举时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更新下面的代码以使用iOS 5的自动引用计数时,在尝试执行快速枚举时为state-> itemPtr分配缓冲区时发生错误,以便可以使用 foreach循环。我得到的错误是将'__autoreleasing id *'分配给'__unsafe_unretained id *'更改指针的保留/释放属性。请参阅带注释的代码行。

While updating the code below to use Automatic Reference Counting for iOS 5, an error is occurring when the "state->itemPtr" is assigned the buffer when trying to perform Fast Enumeration so that the implementing class can be iterated with the "foreach" loop. The error I am getting is "Assigning '__autoreleasing id *' to '__unsafe_unretained id*' changes retain/release properties of pointer". See the line of code with the comment.

/*
 * @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类型的实例变量。

The _tuples variable in this example is an instance variable of type NSMutableArray.

如何解决此错误?

推荐答案

您需要更改缓冲区进入 __ unsafe_unretained

You need to change buffer into __unsafe_unretained:

- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState *)state
                                   objects: (id __unsafe_unretained *)buffer
                                     count: (NSUInteger)bufferSize

来源

编辑:轻松摆脱中的错误

c>

easy way to get rid of the error in mutationPtr:

state->mutationsPtr = &state->extra[0];

这篇关于自动引用计数:快速枚举时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆