• 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
Re: Confusion about KVC and custom setter
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Confusion about KVC and custom setter


  • Subject: Re: Confusion about KVC and custom setter
  • From: Aaron Tuller <email@hidden>
  • Date: Mon, 09 Apr 2012 18:40:07 -0700

you need to override automaticallyNotifiesObserversForKey: to return NO for @"selectedIndex"

see:

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/KeyValueObserving/Articles/KVOCompliance.html

if you observe an object, the runtime automatically generates the willChange/didChange unless you override this method to return NO.

basically you're sending extra willChange/didChange calls here as it is.

-aaron



At 6:28 PM -0700 4/9/12, Rick Mann wrote:
I'm observing a property of one of my classes, "selectedIndex". Even if the value is set to the same value, KVO fires and my observe method gets called, which is something I thought by default it doesn't do.

So I implemented a custom setter for my property, like this:

- (void)
setSelectedIndex: (NSUInteger) inIndex
{
    if (mSelectedIndex == inIndex)
    {
        return;
    }

    [self willChangeValueForKey: @"selectedIndex"];
    mSelectedIndex = inIndex;
    [self didChangeValueForKey: @"selectedIndex"];
}

Sure enough, my method returns early, but KVO still gets called. What am I misunderstanding?

Thanks,
Rick


_______________________________________________

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

_______________________________________________

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

References: 
 >Confusion about KVC and custom setter (From: Rick Mann <email@hidden>)

  • Prev by Date: Confusion about KVC and custom setter
  • Next by Date: Re: -[NSURL path] "If the path has a trailing slash it is stripped"
  • Previous by thread: Confusion about KVC and custom setter
  • Next by thread: Is UIWebView KVO compliant?
  • Index(es):
    • Date
    • Thread