0

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
Ketan P
  • 4,259
  • 3
  • 30
  • 36
Alik Rokar
  • 1,135
  • 1
  • 10
  • 16
  • 2
    `mutableQuery` should be mutable, but `mutableQuery[@"where"]` doesn't have to. You can have info there: http://stackoverflow.com/questions/1950361/deep-mutable-copy-of-a-nsmutabledictionary to create a "deepMutableCopy", (except its prior to ARC) – Larme Jul 08 '16 at 12:43
  • 1
    Check that `currentQuery` is kind of NSDictionary? – Ekta Padaliya Jul 08 '16 at 12:46

1 Answers1

2

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.

Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
Ali Kıran
  • 1,006
  • 7
  • 12