__NSDictionaryI setObject:forKey:崩溃

__NSDictionaryI setObject:forKey: Crash

问题是我正在改变 NSDictionary,但即使在获得 mutableCopy 之后,应用程序也会崩溃。

下面是复制方法

+ (NSMutableDictionary *)updateQuery:(NSMutableDictionary *)currentQuery toSearchAfterAccountPaginationSequence:(NSString *)accountPaginationSequence {

    //try1
    NSMutableDictionary *mutableQuery = [currentQuery mutableCopy];

    //try2 
    NSMutableDictionary *mutableQuery2=[NSMutableDictionary dictionaryWithDictionary:currentQuery];

    //crashes on this line
    mutableQuery[@"where"][@"account_pagination_sequence"] = @{ @"lt" : accountPaginationSequence };

    return mutableQuery;
}

错误日志(应用程序在有限数量的设备上崩溃)

[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance

我想这就是你想要达到的目标

+ (NSMutableDictionary *)updateQuery:(NSMutableDictionary *)currentQuery toSearchAfterAccountPaginationSequence:(NSString *)accountPaginationSequence {

    //try1
    NSMutableDictionary *mutableQuery = currentQuery.mutableCopy;
    NSMutableDictionary *where =  mutableQuery[@"where"].mutableCopy;

    where[@"account_pagination_sequence"] = @{ @"lt" : accountPaginationSequence };
    mutableQuery[@"where"] =  where;

    return mutableQuery;
}

编辑: Objective-C 在对象上调用 mutableCopy 不是 recursive.You 也需要在嵌套对象中调用 mutableCopy。