{"title":"Parse JSON Directly into NSManagedObjects","html_url":"https://deallocatedobjects.com/posts/parse-json-directly-into-nsmanagedobjects","source_url":"http://www.cimgf.com/2012/01/11/handling-incoming-json-redux/","markdown_content":"[Tom Harrington](https://twitter.com/#!/atomicbird) brings us another insanely valuable little bit of code that does some simple, yet awesome things with a parsed NSDictionary of JSON content:\r\n\r\n> - Provide a safe, generic alternative to Cocoa’s -setValuesForKeysWithDictionary: for use with NSManagedObject and its subclasses\r\n- 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.\r\n\r\nBasically 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.\r\n\r\nThe end result is being able to do something like this:\r\n\r\n``` objc\r\n[myObj setValuesForKeysWithDictionary:jsonDict];\r\n```\r\n\r\nI 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.","html_content":"<p><a href=\"https://twitter.com/#!/atomicbird\">Tom Harrington</a> brings us another insanely valuable little bit of code that does some simple, yet awesome things with a parsed NSDictionary of JSON content:</p>\n\n<blockquote>\n<ul>\n<li>Provide a safe, generic alternative to Cocoa’s -setValuesForKeysWithDictionary: for use with NSManagedObject and its subclasses</li>\n<li>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.</li>\n</ul>\n</blockquote>\n\n<p>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.</p>\n\n<p>The end result is being able to do something like this:</p>\n<figure class=\"code\" data-lang=\"objective-c\"><figcaption><span class=\"lang\">Objective-C</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">[myObj </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">setValuesForKeysWithDictionary:</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">jsonDict];</span></span></code></pre></figure>\n<p>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.</p>","created_at":"2013-11-16T05:10:25.585Z","updated_at":"2013-11-16T05:10:25.585Z"}