• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Explanation needed regarding dependent keys
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Explanation needed regarding dependent keys
      • From: Keary Suska <email@hidden>
    • Re: Explanation needed regarding dependent keys
      • From: Scott Stevenson <email@hidden>
  • Prev by Date: Re: key-value support for Objective-C "class" variables?
  • Next by Date: RS: unichars displayed in a table column
  • Previous by thread: Re: Posting Leopard Source Code
  • Next by thread: Re: Explanation needed regarding dependent keys
  • Index(es):
    • Date
    • Thread