Someone on IRC told me I could create objective c objects directly from plist files in such a way that I don’t have to specify strings everywhere for serialization/deserialization of the plist.
Does anyone know how to do this?
I’d like to simply have a plist like this
<array>
<object>
<id>my unique id</id>
<description>first items description</description>
</object>
</array>
And somehow deserialize this directly into objective c objects without having to specify the literal string “description” or “id” anywhere in code.
This may not be possible, but I couldn’t find anywhere online or on SO that did this or said it’s not possible.
Also, I wanted to follow through on what the guy on IRC guy said, but didn’t have time to ask him there n then.
Any help is greatly appreciated,
-Vandelizer
You’re looking for class serialization. Apple has a guide here on it’s object archival methods. Some keywords for searching are “archival” “NSCoder” and “serialization”.
Basically you want to have your class implement the NSCoding interface. You then create the methods
-(void)encodeWithCoder:(NSCoder*)coderand- (id)initWithCoder:(NSCoder *)coder. The former is for writing data and the latter will create an object from the data. See the “Encoding and Decoding Objects” section for how you would implement these methods.Then, when you want to write to a file, you would make an NSArray of your objects, and call
[NSKeyedArchiver archiveRootObject:toFile:]to write the array to a file, and[NSKeyedUnarchiver unarchiveObjectWithFile:]to read the file.