Alik Rokar Alik Rokar - 4 months ago 34
Objective-C Question

__NSDictionaryI setObject:forKey: Crash

The problem is that I am mutating a

NSDictionary
, but even after getting a
mutableCopy
, the app crashes.

Below is Method for copy :

+ (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;
}


Error Log (the app crashes on a limited amount of devices)

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

Answer

i think this is what you trying to achieve

+ (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;
}

Edit: In Objective-C calling mutableCopy on objects is not recursive.You need to call mutableCopy in nested objects too.