Re: Writing Array of Dictionaries to Defaults (CFPreferences)
Re: Writing Array of Dictionaries to Defaults (CFPreferences)
- Subject: Re: Writing Array of Dictionaries to Defaults (CFPreferences)
- From: "email@hidden" <email@hidden>
- Date: Tue, 2 Dec 2008 13:42:08 -0600
Thanks Steven-
That's a helpful example, but does not work in my example.
From my research of Apple's docs, NSUserDefaults, and the
SharedDefaultsController can only save defaults to ~/Library/
Preferences and for the app that is running it (in your example case
com.yourcompany.dictInDefaults, in mine
com.ryanharter.speakermutepref), but since this is running from System
Preferences, and the preferences are actually for a different
application (my daemon and helper, com.ryanharter.speakermute and
com.ryanharter.speakermutehelper respectively) and the defaults need
to be stored in /Library/Preferences since they are not user specific
I need to use CFPreferences.
That's why I'm not using NSUserDefualts, but please let me know if my
information is inaccurate.
Any other thoughts?
Thanks
Ryan
On Dec 2, 2008, at 9:08 AM, Steven Riggs wrote:
Here's an example of how to do it with no code...
http://www.stevenriggs.com/Site/Cocoa_Programming.html
Good luck,
Steven Riggs
On Dec 1, 2008, at 8:08 PM, email@hidden wrote:
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:
@me.com
This email sent to email@hidden
_______________________________________________
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