for each loop in Objective-C for accessing NSMutable dictionary

for (NSString* key in xyz) { id value = xyz[key]; // do stuff } This works for every class that conforms to the NSFastEnumeration protocol (available on 10.5+ and iOS), though NSDictionary is one of the few collections which lets you enumerate keys instead of values. I suggest you read about fast enumeration in the … Read more

What is an NSCFDictionary?

NSDictionary and the other collection classes are actually class clusters: several concrete subclasses classes masquerading under the interface of a single class: they all provide the same functionality (because they are subclasses of the same class — in NSDictionary’s case, this involves the three “primitive methods” -count, -objectForKey:, and -keyEnumerator), but have different internal workings … Read more

Serialize and Deserialize Objective-C objects into JSON

Finally we can solve this problem easily using JSONModel. This is the best method so far. JSONModel is a library that generically serialize/deserialize your object based on Class. You can even use non-nsobject based for property like int, short and float. It can also cater nested-complex JSON. Considering this JSON example: { “accounting” : [{ … Read more

Getting NSDictionary keys sorted by their respective values

The NSDictionary Method keysSortedByValueUsingComparator: should do the trick. You just need a method returning an NSComparisonResult that compares the object’s values. Your Dictionary is NSMutableDictionary * myDict; And your Array is NSArray *myArray; myArray = [myDict keysSortedByValueUsingComparator: ^(id obj1, id obj2) { if ([obj1 integerValue] > [obj2 integerValue]) { return (NSComparisonResult)NSOrderedDescending; } if ([obj1 integerValue] … Read more

How can I sort an NSArray containing NSDictionaries?

Here is an example. Imagines you have an array of dictionnaries. Ecah dictionnary have 2 keys “name” & “dateOfBirth”. You can sort your array by “name” like this : // // Sort array by name // NSSortDescriptor *Sorter = [[NSSortDescriptor alloc] initWithKey:@”name” ascending:YES]; [myArray sortUsingDescriptors:[NSArray arrayWithObject:Sorter]]; [Sorter release]; Note that myArray is a NSMutableArray.