• 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
Strange behavior of observeValueForKeyPath:ofObject:change:context:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Strange behavior of observeValueForKeyPath:ofObject:change:context:


  • Subject: Strange behavior of observeValueForKeyPath:ofObject:change:context:
  • From: WT <email@hidden>
  • Date: Tue, 6 Jan 2009 15:55:59 +0100

Hello,

in the app I'm writing, I have a custom view with several KVO- compliant properties which, when changed, should cause the view to redraw itself. I've then set up a dependent key (displayNeeded) and have my custom view KV-observe changes to that key. When a change happens, I call setNeedsDisplay. The relevant code appears below:

This is in the -initWithFrame: method of my custom view:

[self addObserver: self
       forKeyPath: @"displayNeeded"
          options: (NSKeyValueObservingOptionNew |
                    NSKeyValueObservingOptionOld)
          context: NULL];

And these are the two methods that are needed to make KVO work for the dependent key in question:

+ (void) initialize
{
[self setKeys: [NSArray arrayWithObjects: @"foo", @"bah", @"etc", nil]
triggerChangeNotificationsForDependentKey: @"displayNeeded"];
}


(Yes, I know that -setKeys:triggerChangeNotificationsForDependentKey: is deprecated in Leopard)

- (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object
change: (NSDictionary *) change context: (void *) context;
{
id oldValue = [change objectForKey: NSKeyValueChangeOldKey];
id newValue = [change objectForKey: NSKeyValueChangeNewKey];


    NSLog(@"old = %@", oldValue);
    NSLog(@"new = %@", newValue);

    if (! [newValue isEqual: oldValue])
    {
        if ([keyPath isEqual: @"displayNeeded"])
        {
            NSLog(@"displayNeeded");
            [self setNeedsDisplay: YES];
        }
        else
        if ([keyPath isEqual: @"AnotherDependentKey"])
        {
            // Handle another dependent key.
        }
    }
}

The reason for the comparison between the old and new values should be obvious, namely, I don't want to redraw the screen unless the value of the original key has in fact changed.

Now, here's the problem: when I run my app, both the old and new values are logged as simply 0 (zero). That's clearly incorrect because (a) not all of my properties are numbers (some are colors, for example) and (b) I know I've made a change and yet the old and new values come out equal.

Everything compiles fine, with no warnings of any kind, and the app runs fine if I remove the comparison between old and new (but the old and new values are still logged as zero).

Anyone has any idea of what I may be doing wrong?

Thanks in advance.
Wagner
_______________________________________________

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: Strange behavior of observeValueForKeyPath:ofObject:change:context:
      • From: Quincey Morris <email@hidden>
  • Prev by Date: NSUIHeartBeat gets stuck
  • Next by Date: Re: Binary search on sorted NSArray in Cocoa?
  • Previous by thread: Artur Zaiat/MOL/CEECIS/UNICEF is out of the office.
  • Next by thread: Re: Strange behavior of observeValueForKeyPath:ofObject:change:context:
  • Index(es):
    • Date
    • Thread