Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key-Value Coding

I had a straight forward approach of turning Key/Value pairs of an XML excerpt into an NSDictionary when I started a simple iPhone application. The problem is, I need to turn those NSDictionary's instances that once populated my UITableView's into custom classes because they require behavior and additional complexity. The problem here is that now in order for me to instantiate an object and fill its instance variables with key/value pairs from a web service becomes that much more difficult. I can no longer throw it into a method that iterates through the XML and uses KVC to set its instance variables.

What kind of other solution is out there?

like image 268
Coocoo4Cocoa Avatar asked Dec 18 '08 20:12

Coocoo4Cocoa


2 Answers

You can still use key value coding methods on your custom class, as long as you name your variables appropriately there's no difference there. With that being said though, when I'm working with XML I usually end up testing each node name or creating a key lookup table, since the names in the data source I'm working with aren't key value coding compliant. If you have control over the data source though, you could just continue to use setValue:forKey:.

I'd recommend reading this guide about key value coding if you haven't already. It's fundamental to many great tools in Cocoa.

like image 92
Marc Charbonneau Avatar answered Oct 05 '22 23:10

Marc Charbonneau


Look into NSCoding. You can use the NSCoding protocol to save your object, properties and all, as data.

Once your object is NSCoding compliant, you can just archive the whole array of objects using NSKeyedArchiver.

Please note that if you have a large number of objects, this can dramatically affect the app's performance on the iPhone during load and save.

like image 26
August Avatar answered Oct 06 '22 00:10

August