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: Ken Thomases <email@hidden>
- Date: Tue, 2 Dec 2008 22:53:07 -0600
On Dec 1, 2008, at 7:08 PM, email@hidden wrote:
// 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]];
What are you doing here? You have an object (anObject). Presumably
you think it's an NSNumber. Then, you ask for its intValue, which
returns... an int. An int is _not_ an object, of course. You then
cast that int to an NSNumber*, which is almost certainly wrong. You
then pass it to some method which is probably expecting a pointer to
an actual object, not an int posing as a pointer.
}
// 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]];
You do the same here.
}
// 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;
You have declared a pointer, but you haven't set it to point to
anything, let alone anything meaningful. It just contains garbage at
this point.
for (Schedule *schedules in scheduleArray) {
[tmpArray addObject:[schedules toDictionary]];
You're calling methods on an uninitialized pointer. This will
probably crash. If you're unlucky, the bug will be hidden by
happenstance (tmpArray may happen to be nil because that's what was
left on the stack).
You needed to initialize tmpArray to point to an actual NSMutableArray:
NSMutableArray* tmpArray = [NSMutableArray array];
NSLog(@"Adding this to array %@", [schedules toDictionary]);
}
// Write the array to preferences in /Library/Preferences.
CFPreferencesSetValue(ScheduleKey, (CFArrayRef)tmpArray,
ApplicationID, kCFPreferencesAnyUser, kCFPreferencesCurrentHost);
Again, tmpArray doesn't contain anything meaningful. So, attempting
to write it out will have undefined results -- probably the crash
you're seeing.
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?
Your problem isn't in writing stuff to preferences. Your problem is
that you're not constructing valid objects. I think you need to
review the basics of C, Objective-C, and Cocoa. This is pretty
fundamental stuff.
Regards,
Ken
_______________________________________________
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