I am serialising some JSON data into a dictionary. The dictionary contains a "status" object, and an array of dictionaries (one key of which is "time_dropped", by which I want to sort the array).
However, I get an exception claiming that a "mutating method [was] sent to [an] immutable object". Here's the relevant piece of code. I did declare pins
as a NSMutableArray
, by the way.
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&localError];
NSString *status = [dataDictionary objectForKey:@"status"];
NSLog(@"status: %@", status);
if ([status isEqualToString:@"Okay"]) {
self.pins = (NSMutableArray *) [dataDictionary objectForKey:@"pins"];
NSSortDescriptor *timeDescriptor = [NSSortDescriptor
sortDescriptorWithKey:@"time_dropped"
ascending:YES selector:@selector(compare:)];
NSSortDescriptor *titleDescriptor = [NSSortDescriptor
sortDescriptorWithKey:@"title"
ascending:YES selector:@selector(caseInsensitiveCompare:)];
NSArray *descriptors = @[timeDescriptor, titleDescriptor];
[self.pins sortUsingDescriptors:descriptors];