OK, so I’m implementing a classic scenario :
- A
NSPopupButtonwith some items in it - When the selected value changes, my
itemsArrayis updated - The
itemsArrayis linked to anNSArrayController - Each item in the
itemsArrayis anNSMutableDictionary(with keys :title,content) - An
NSTableViewdisplays thetitles of thearrangedObjects(binding) - An
NSTextViewdisplays thecontentof the selected item.
Now, what I want is to automatically save any changes to the itemsArray (or itemsArray’s item title/content), but without using Core Data (which I suspect might have been the best way to go about it).
I imagine it’s quite a basic question this one, but honestly I’ve never really like Cocoa’s auto-magical way of doing things… so, I need your help…
How should I go about that?
You can write an array to a file very easily:
This will work if all the contents of the array are property list objects (i.e. they are
NSNumber,NSString,NSDictionary,NSArrayorNSDataobjects). This is the case in your example.You can then recreate the array using either the
arrayWithContentsOfURL:orinitWithContentsOfURL:methods when you load from disk.If your model object is more complex than just an array, then you should make your model object conform to the
NSCodingprotocol. This means you need to implement theinitWithCoder:andencodeWithCoder:methods. You can then use theNSKeyedArchiverandNSKeyedUnarchiverclasses to convert your object to and from anNSDatarepresentation that you can write to disk.You should read the Archives and Serialization Programming Guide for more detailed information.