How to: Where's the difference between setObject:forKey: and setValue:forKey: in NSMutableDictionary?
Where’s the difference between setObject:forKey: and setValue:forKey: in NSMutableDictionary?
When looking at the documentation, I hardly see any big difference. Both “value” and “object” are of type id, so can be any object. Key is once a string, and in the other case an id. One of them seems to retain the object, and the other don’t. What else? Which one is for what case?
Sorting a NSMutableArray containing NSDictionary by a key inside the NSDictionary
So, when you have an NSMutableArray of UserElements which are contained in NSDictionaries and now if you want to sort it with reference to a key inside these Dictionaries, for example LAST NAME, then, you can acheive it with the help of NSSortDescriptors...
I have been working on a project for a little while now and have been working with constant changing data. The problem I was running into was when a new type of entry was created, I would have to go into the code, make a change, and if it was ready for the app store, then submit it as an update. This was not going to be an efficient way to handle the added types.
I created a JSON file that contains the template and the data mapping in a format like so:
"signup": {
"template": "{{user1}} just signed up. Welcome!",
"dataMap": {
"user1": "user.screen_name"
}
}
The dataMap represents the location in dot notation as to where the data could be retrieved from the NSMutableDictionary. So the idea was the split the dataMap into an Array and then figure out some way to create something like this dynamcially:
Would have been fine if the dataMap was consistant on only having 2 Keys, but I needed it more dynamic than that.
Now enters, ValueForKeyPath.
This has got to be one of the coolest parts to using an NSMutableDictionary. I can now pass it like below and it will do the same thing as the example above.
[Dictionary valueForKeyPath:@"user.screen_name"]
Isn't that just the coolest thing ever? Ok, maybe to me it is, cause I can see using this for numerous projects down the road, to help reduce the amount of code actually needed.
I look forward to posting more of these as I come across cool ideas and what not.