how to dealloc a NSFormatter-subclass
how to dealloc a NSFormatter-subclass
- Subject: how to dealloc a NSFormatter-subclass
- From: "Dr. Jan-Hendrik Dörner" <email@hidden>
- Date: Thu, 16 Mar 2006 15:28:04 +0100
Hi,
I would like to allow only an optional "+" following any number of
digits on a NSTextField in a
preference pane. I think the corresponding regular expression would
be something like
/^\+?[0-9]*$/ . Since this cannot be easyly handled by any standard
method, I linked a custom NSFormatter-class
to an NSTextField with InterfaceBuilder.
A solution which works for me is
- (BOOL)isPartialStringValid:(NSString *)partialString
newEditingString:(NSString **)newString errorDescription:(NSString **)
error{
if(NSEqualRanges([([partialString hasPrefix:@"+"]?[partialString
substringFromIndex:1]:partialString)
rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet]
invertedSet]], NSMakeRange(NSNotFound, 0)))
return TRUE;
NSBeep();
return FALSE;
}
But a problem I am experiencing is:
1) if the modified NSTextField is left with a "tab" to the next
NSTextField, everything works as expected, but
2) if the modified NSTextField is left by clicking an "OK" button, my
function
- (NSString *)stringForObjectValue:(id)anObject
is called with the last "tab-out" value and not with the current
value inside the linked NSTextField (to which the NSTextField is than
automatically reset to).
So I decided to store the right value in a local Variable "stringValue".
- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string
errorDescription:(NSString **)error{
if(stringValue != NULL) {[stringValue release]; stringValue = NULL;}
if(string != NULL) stringValue = [[NSString alloc]
initWithString:string];
else stringValue = [[NSString alloc] initWithString:@""];
*obj = stringValue;
return YES;
}
This works now as expected with my function
- (NSString *)stringForObjectValue:(id)anObject{
return ( anObject == nil ) ? [NSString stringWithString:@""] :
([anObject isKindOfClass: [NSString class]] ? stringValue: nil);
}
But beeing a carefull developer, I need to release "stringValue" once
the preference Pane is deallocated.
- (void) dealloc{
NSLog(@"Dealloc Substitute Formatter");
if(stringValue != NULL) [stringValue release];
[super dealloc];
}
But unfortunately it seems that the dealloc-function is never called.
(I do not see any message on the console!)
It does not matter if I leave, quit or even remove my preferencePane.
What am I doing wrong here?
_______________________________________________
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