Xamarin.Mac NSUndoManager with NSTableView: No NSArrayController

After coding the example in the last couple of posts, I decided to implement the same app without using an NSArrayController and bindings, but rather using a List for my data. This has the advantage of being more cross platform, of course. However as I still wanted to use the saving mechanism built into Cocoa, I just had to convert my List to an NSMutableArray before saving and then archive with the NSKeyedArchiver. Of course I then had to convert the NSMutableArray back into a List after using NSKeyedUnarchiver to retrieve the NSMutableArray that was archived.

The best way I found to create the NSMutableArray was to just copy the elements from the Employees List to a new NSMutableArray:

NSMutableArray array = new NSMutableArray();
foreach (Person p in Employees) {
NSData data = NSKeyedArchiver.ArchivedDataWithRootObject(array);

Then to get that NSMutableArray back to a List:

NSMutableArray newArray = (NSMutableArray)NSKeyedUnarchiver.UnarchiveObject(data);
Employees = NSArray.FromArray(newArray).ToList();

The real load method has a try/catch block to catch any errors loading in the file and unarchiving the data. but that’s the method to convert an MSMutableArray back to a list that I used.

You can download the revised RaiseMan example here: RaiseManNoArrayController

Leave a Reply

Your email address will not be published. Required fields are marked *