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

How can we store into an NSDictionary? What is the difference between NSDictionary and NSMutableDictionary?

The NSDictionary and NSMutableDictionary docs are probably your best bet. They even have some great examples on how to do various things, like… …create an NSDictionary NSArray *keys = [NSArray arrayWithObjects:@”key1″, @”key2″, nil]; NSArray *objects = [NSArray arrayWithObjects:@”value1″, @”value2″, nil]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; …iterate over it for (id key in dictionary) { … Read more

Sort an NSMutableDictionary

Get the Array of the Values, sort that array and then get the key corresponding to the value. You can get the values with: NSArray* values = [myDict allValues]; NSArray* sortedValues = [values sortedArrayUsingSelector:@selector(comparator)]; But, if the collection is as you show in your example, (I mean, you can infer the value from the key), … Read more

how to do true deep copy for NSArray and NSDictionary with have nested arrays/dictionary?

A couple of years ago, I wrote a few category methods for exactly the same reason, transforming a whole tree of user defaults to mutable. Here they are – use them at your own risk! 🙂 // // SPDeepCopy.h // // Created by Sherm Pendley on 3/15/09. // #import <Cocoa/Cocoa.h> // Deep -copy and -mutableCopy … Read more

Where’s the difference between setObject:forKey: and setValue:forKey: in NSMutableDictionary?

setValue:forKey: is part of the NSKeyValueCoding protocol, which among other things, lets you access object properties from the likes of Interface Builder. setValue:forKey: is implemented in classes other than NSDictionary. setObject:forKey: is NSMutableDictionary’s reason to exist. Its signature happens to be quite similar to setValue:forKey:, but is more generic (e.g. any key type). It’s somewhat … Read more

tech