Writing Array of Dictionaries to Defaults (CFPreferences)
Writing Array of Dictionaries to Defaults (CFPreferences)
- Subject: Writing Array of Dictionaries to Defaults (CFPreferences)
- From: "email@hidden" <email@hidden>
- Date: Mon, 1 Dec 2008 19:08:10 -0600
Hey All-
I'm working on a Preference Pane for my app (System Preferences
plugin) and need to write an array of dictionaries pulled from my
NSTableView to defaults. I don't want to serialize it as data since I
would like it to be editable from the command line as well (for easy
network administration). I'm using CFPreferences because the
preferences need to be read by the plugin, my daemon which runs as
root, and a helper app the the daemon fires off for gui interaction
and I therefore want the plist to be located in /Library/Preferences/.
I've got strings and boolean values being written there just fine, but
I can't seem to create this array of dicts and have it put anything in
the plist.
Here's my code:
// Notifies of change of data in table view. Make sure to reflect
changes in datasource.
- (void)tableView:(NSTableView *)aTableView setObjectValue:
(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:
(NSInteger)rowIndex
{
NSString *columnID = [aTableColumn identifier];
NSMutableArray *scheduleArray = [NSMutableArray
arrayWithArray:_schedules];
// If deactivate time was edited, we create a new schedule with just
that changed.
if ([columnID isEqualToString:kScheduleDeactivateString]) {
NSLog(@"Deactivate anObject = %@", anObject);
[[scheduleArray objectAtIndex:rowIndex] setDeactivate:(NSNumber *)
[anObject intValue]];
}
// If activate time was edited, we create a new schedule with just
that changed.
else if ([columnID isEqualToString:kScheduleActivateString]) {
NSLog(@"Activate anObject = %@", anObject);
[[scheduleArray objectAtIndex:rowIndex] setActivate:(NSNumber *)
[anObject intValue]];
}
// If name was edited, we create a new schedule with just that changed.
else if ([columnID isEqualToString:kScheduleNameString]) {
NSLog(@"Title anObject = %@", anObject);
[[scheduleArray objectAtIndex:rowIndex] setTitle:(NSString
*)anObject];
}
// Iterate through the array to create a new one of NSDictionaries,
not schedules.
NSMutableArray *tmpArray;
for (Schedule *schedules in scheduleArray) {
[tmpArray addObject:[schedules toDictionary]];
NSLog(@"Adding this to array %@", [schedules toDictionary]);
}
// Write the array to preferences in /Library/Preferences.
CFPreferencesSetValue(ScheduleKey, (CFArrayRef)tmpArray,
ApplicationID, kCFPreferencesAnyUser, kCFPreferencesCurrentHost);
CFPreferencesSynchronize(ApplicationID, kCFPreferencesAnyUser,
kCFPreferencesCurrentHost);
}
The pref pane runs fine. I can add items which calls my addSchedule
method and just adds a blank item to the array. When I edit the
title, it appears to work fine from the gui, but nothing is written to
the plist. If I edit the activate or deactivate fields then I get the
following error and System Preferences crashes.
12/1/08 6:39:59 PM System Preferences[2301] Adding this to array {
activate = 100;
deactivate = 1130;
name = "New Schedule";
}
12/1/08 6:39:59 PM System Preferences[2301] *** -[NSCFString
stringValue]: unrecognized selector sent to instance 0x16e63740
Does anyone have any insight about writing array's of dicts to
preferences?
Thanks
Ryan Harter
http://www.ryanharter.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden