Modifying an object managed by NSArrayController
Modifying an object managed by NSArrayController
- Subject: Modifying an object managed by NSArrayController
- From: Paul Collins <email@hidden>
- Date: Wed, 20 Dec 2006 18:48:55 -0800
Q: Have I been using an undefined/unsupported behavior of
NSArrayController, or is this legitimate: Can I obtain an (already
mutable) instance from the array via -selectedObjects, and make
changes to it, assuming that it will continue to be part of the array?
I'm running a modal dialog to allow the user to edit the object
before setting the change. This became a problem when something I
added to the dialog caused the NSArrayController to reload its data,
thus making my reference to the data instance obsolete in terms of
NSArrayController's data. So that's bad. But if I'm careful not to
disturb the underlying data, is this still unsafe?
Here's the code - real, but with a lot of presumably unrelated stuff
removed...
IBOutlet NSArrayController *iServicesCntrllr;
- (IBAction)editService:(id)sender {
NSArray *sel = [iServicesCntrllr selectedObjects];
NSMutableDictionary *service;
service = [sel objectAtIndex:0];
ODServiceDialog *dialogger = [[ODServiceDialog alloc] init];
// Run user edit dialog -
service = [dialogger runServiceDialog:service];
[dialogger release];
}
@interface ODServiceDialog : NSObject
{
IBOutlet NSTextField *iName;
IBOutlet NSPanel *iPanel;
NSMutableDictionary *iServiceToBeEdited; // belongs to services
array, if you change it, it's changed there (but we retain in
runServiceDialog just in case)
}
@end
@implementation ODServiceDialog
- (NSMutableDictionary *)runServiceDialog:(NSMutableDictionary *)
aServiceDict {
int rtn;
iServiceToBeEdited = [aServiceDict retain]; //keep around in ivar,
although it should survive runModalForWindow anyway
[iName setStringValue:[aServiceDict objectForKey:@"name"]];
rtn = [NSApp runModalForWindow:iPanel];
[iPanel close]; // As an NSPanel, it's not released at this point,
so we can still get value
if (rtn == NSRunStoppedResponse) {
[aServiceDict setObject:[iName stringValue] forKey:@"name"];
}
}
--Paul Collins
** Gracion Software LLC http://www.gracion.com/
** Tel: +1 (541) 488-3488 iChat/AIM/Skype: gracionsoft
** DigiTunnel 1.4 PPTP/VPN client for OS X
** Pronto Patent - search/download US patents fast
** And check out: http://www.opendoor.com/doorstop/
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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