Question

OK, so I'm implementing a classic scenario :

  • A NSPopupButton with some items in it
  • When the selected value changes, my itemsArray is updated
  • The itemsArray is linked to an NSArrayController
  • Each item in the itemsArray is an NSMutableDictionary (with keys : title,content)
  • An NSTableView displays the titles of the arrangedObjects (binding)
  • An 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?

Was it helpful?

Solution

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.

OTHER TIPS

Another solution might be to add a Shared User Defaults Controller and bind the Controller Content Array from the Array Controller to the Shared User Defaults Controller

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top