My name is Jake Marsh.
I'm a developer, designer, and writer.

Subscribed via Push Notifications. You can also subscribe via RSS or Twitter.

Subscribe via RSS or Twitter.

Subscribe via Push Notifications, RSS or Twitter.

▸ Parse JSON Directly into NSManagedObjects

Tom Harrington brings us another insanely valuable little bit of code that does some simple, yet awesome things with a parsed NSDictionary of JSON content:

  • Provide a safe, generic alternative to Cocoa’s -setValuesForKeysWithDictionary: for use with NSManagedObject and its subclasses
  • Handle cases where JSON data didn’t match up with what the managed objects expected. Getting a string where you expect a numeric value, or vice versa, for example, or getting a string representation of a date when you want a real NSDate object.

Basically Tom is examining his managed object's defined properties, and then looking into the NSDictionary he got when he parsed some JSON, and if any of the property key names exist in both structures, he sets the value.

The end result is being able to do something like this:

[myObj setValuesForKeysWithDictionary:jsonDict];

I love little tools and case studies like this as they always help to enlighten more of us to the awesomeness that is the Objective-C runtime.

Vote on Hacker News