【问题标题】:Error with fast enumeration on NSDictionaryNSDictionary 上的快速枚举错误
【发布时间】:2011-11-06 14:37:29
【问题描述】:

感谢这个问题/答案Automatic Reference Counting: Error with fast enumeration 我解决了一部分让我发疯的问题。

但是,我仍然收到错误消息“Assigning 'NSDictionary *__strong ' to '__unsafe_unretained id' changes retain/release properties of pointer”。我已经阅读了 ARC 编程指南和与指针转换相关的部分,但不幸的是,我还不是一个足够好的 c 程序员。

查看带有注释的代码行。任何帮助将不胜感激。提前致谢。

- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState *)state
                               objects: (id __unsafe_unretained *)buffer
                                 count: (NSUInteger)bufferSize
{
    if (*enumRows = [self getPreparedRow]) {
        state->itemsPtr = enumRows; // Assigning 'NSDictionnary *__strong *' to '__unsafe_unretained id*' changes retain/release properties of pointer
        state->state = 0;
        state->mutationsPtr = &state->extra[0]; // was state->mutationsPtr = (unsigned long *) self; before
        return 1;
    } else {
        return 0;
    }
}

在 .h 文件中使用此声明

@interface BWDB : NSObject <NSFastEnumeration> {
    NSDictionary * enumRows[1];
}

【问题讨论】:

    标签: objective-c ios5 automatic-ref-counting


    【解决方案1】:

    这将解决它。

    @interface BWDB : NSObject <NSFastEnumeration> {
         __unsafe_unretained NSDictionary *enumRows[1];
    }
    

    根据您发布的代码,这应该是非常安全的,因为您每次都在检查之前分配它。事实上,根本没有必要将其设为实例变量。

    你可以这样做:

    {
        __unsafe_unretained NSDictionary *enumRows[1];
        if ((*enumRows = [self getPreparedRow])) {
            state->itemsPtr = NULL;
            state->itemsPtr = enumRows;
            state->state = 0;
            state->mutationsPtr = &state->extra[0];
            return 1;
        } else {
            return 0;
        }
    }
    

    我应该注意到,拥有一个指针[] 类型的 NSDictionaries 数组并没有多大价值。简单地使用 NSArray 来保存您的 NSDictionaries 会更有利。

    【讨论】:

    • 非常感谢,它做到了,我很感激。
    • @brad 很高兴为您提供帮助。如果答案解决了您的问题,您应该接受它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    相关资源
    最近更新 更多