Explanation needed regarding dependent keys
Explanation needed regarding dependent keys
- Subject: Explanation needed regarding dependent keys
- From: Michael Norris <email@hidden>
- Date: Sat, 28 Jul 2007 23:55:08 +0200
Hi there
I can't quite get my head around something regarding dependent keys
and the setKeys: triggerChangeNotificationsForDependentKey: method.
In my project, I have an AppController object which contains a
formatted string ("text", an NSMutableAttributedString) and an
unformatted string ("unformattedText", an NSString). These two
strings are dependent on each other: they can both be altered in the
UI (the former through an NSTextView with its "attributedString"
bound to "text", and the latter through an NSTextField with its
"value" bound to "unformattedText"). (The reason for these two
versions of the same string is that eventually the unformatted string
will be listed in a table column, which requires NSStrings, while the
formatted version will appear in an inspector, and allow different
fonts/colours etc).
Below is the first code I wrote, which does *not* correctly update
the NSTextView when I change the NSTextField. If, however, I edit the
NSTextView, it does correctly update the NSTextField.
#import "AppController.h"
@implementation AppController
+ (void)initialize {
[self setKeys: [NSArray arrayWithObjects:@"text", nil]
triggerChangeNotificationsForDependentKey:@"unformattedText"];
[self setKeys: [NSArray arrayWithObjects:@"unformattedText", nil]
triggerChangeNotificationsForDependentKey:@"text"];
}
- (id)init {
if (self = [super init]) {
text = [[NSMutableAttributedString alloc] initWithString:@""];
[self setUnformattedText:@"test"];
}
return self;
}
- (NSString*)unformattedText { return unformattedText; }
- (NSMutableAttributedString*)text { return text; }
- (int)alignment { return alignment; }
- (void)setUnformattedText:(NSString*)str {
[str retain];
[unformattedText release];
unformattedText = str;
[text replaceCharactersInRange:NSMakeRange(0,[text length])
withString:unformattedText];
}
- (void)setText:(NSMutableAttributedString*)str {
[str retain];
[text release];
text = str;
[unformattedText release];
unformattedText = [[str string] retain];
}
@end
To make this work, I had to change the last two lines of the
"setUnformattedText" method:
- (void)setUnformattedText:(NSString*)str {
[str retain];
[unformattedText release];
unformattedText = str;
[text release]; // CHANGED!
text = [[NSMutableAttributedString alloc]
initWithString:unformattedText]; // CHANGED!
}
Can anyone explain to me why it only updates the NSTextView *if* I go
through the hoop of releasing the "text"object, instead of just doing
mutating it? Is this documented somewhere? Why does the NSTextView
care whether my ivar has been released or not? Doesn't setKeys:
triggerChangeNotificationsForDependentKey: ensure that the NSTextView
will reload its attributedString key?
Thanks for your time,
Michael
_______________________________________________
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