OK, so I'm implementing a classic scenario :
NSPopupButton
with some items in ititemsArray
is updateditemsArray
is linked to an NSArrayController
itemsArray
is an NSMutableDictionary
(with keys : title
,content
)NSTableView
displays the title
s of the arrangedObjects
(binding)NSTextView
displays the content
of 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:
[yourArray writeToURL:someFileURL atomically:YES];
This will work if all the contents of the array are property list objects (i.e. they are NSNumber
, NSString
, NSDictionary
, NSArray
or NSData
objects). This is the case in your example.
You can then recreate the array using either the arrayWithContentsOfURL:
or initWithContentsOfURL:
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 NSCoding
protocol. This means you need to implement the initWithCoder:
and encodeWithCoder:
methods. You can then use the NSKeyedArchiver
and NSKeyedUnarchiver
classes to convert your object to and from an NSData
representation that you can write to disk.
You should read the Archives and Serialization Programming Guide for more detailed information.