Re: "Default" Value In Object
Re: "Default" Value In Object
- Subject: Re: "Default" Value In Object
- From: mmalcolm crawford <email@hidden>
- Date: Fri, 17 Jun 2005 23:01:26 -0700
On Jun 17, 2005, at 8:27 PM, Thaddeus Cooper wrote:
@implementation AnEntry
+ (void) initialize {
if (self == [AnEntry class]) {
NSArray *keys = [NSArray arrayWithObjects:
@"scene", @"take", nil];
[self setKeys:keys
triggerChangeNotificationsForDependentKey:@"scene"];
}
}
- (void) awakeFromInsert {
//static int tempTake = 1;
NSString *sTake;
sTake = [[NSNumber numberWithInt:tempTake] stringValue];
[self setValue:sTake forKey:@"take"];
tempTake++;
[self setValue:tempScene forKey:@"scene"];
}
- (NSString *) updateScene {
tempScene = [[NSString alloc] initWithString:[self
valueForKey:@"scene"]];
Note that, by standard Cocoa conventions, this would generate a
memory leak:
tempScene = [[[NSString alloc] initWithString:[self
valueForKey:@"scene"]] autorelease];
return tempScene;
}
@end
I expected that the updateScene method would get called when I
changed the value in the interface, but it never gets called.
It's not clear from the above why you would expect updateScene to be
called/
Note also that the pattern illustrated in the NSPersistentDocument
tutorial for incrementing a value is purely illustrative. It will
only make sense for the first run of the application -- on subsequent
launches the static value will start again from 1...
To elaborate on the approach I suggested before, you could implement
a newObject method in your array controller subclass that does
something like:
- (id)newObject {
NSManagedObect *newObject = (NSManagedObect *)[super newObject];
[newObject setValue:lastSetValue forKey:@"keyName"];
return newObject;
}
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
[self setLastSetValue:[[aNotification object] stringValue]];
}
- (NSString *)lastSetValue { return lastSetValue; }
- (void)setLastSetValue:(NSString *)aLastSetValue
{
if (lastSetValue != aLastSetValue) {
[lastSetValue release];
lastSetValue = [aLastSetValue copy];
}
}
lastSetValue might be a local variable as suggested here, or you
could store it in user defaults. You could also maintain it as
document- or application-wide state, and add it to the metadata for
the persistent store on save.
mmalc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden