super observeValueForKeyPath:keyPath

76 views
Skip to first unread message

Brent Rowland

unread,
Sep 15, 2011, 7:46:13 PM9/15/11
to Xcode Phoenix
In reference to last evening's discussion:


"...unconditionally calling [super observeValueForKeyPath:...] is dangerous. For example, the NSObject implementation throws an exception, so following Apple’s documentation will result in runtime errors."

Jiva.right++;


Brent

Brian Webster

unread,
Sep 15, 2011, 8:12:53 PM9/15/11
to xcodep...@googlegroups.com
Interesting, I guess my code never generated such an exception because I would only pass along that my object _doesn't_ handle itself up to super. Since NSObject doesn't make any such registrations, no such calls ever came in, so I never saw such an exception thrown as a result. Of course Apple's documentation doesn't actually say what NSObject's implementation does, so that's... helpful. :-\

I'd still recommend calling super, but only for any notifications that you _weren't_ the one to sign up for (i.e. don't _unconditionally_ pass everything along to super). You could skip it if you inherit directly from NSObject, but calling super would also serve as a bit of a sanity check, since if you end up passing along a notification that actually _was_ yours, you'll get an exception thrown and know that something is wrong.

And of course the entire discussion is moot if you just use KVO+Blocks instead. :-)

Oh yeah, and here are the links to the versions of the code I was referring to in the presentation:

My version of KVO+Blocks: https://gist.github.com/1220613
Andy Matuschak's original version: https://gist.github.com/153676
Jonathan Wight's KVONotificationManager: https://github.com/schwa/KVO-Notification-Manager

--
Brian Webster
bewe...@gmail.com

> --
> You received this message because you are subscribed to the Google Groups "XCodePhoenix" group.
> To post to this group, send email to xcodep...@googlegroups.com.
> To unsubscribe from this group, send email to xcodephoenix...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/xcodephoenix?hl=en.

Brad Miller

unread,
Sep 15, 2011, 8:23:34 PM9/15/11
to xcodep...@googlegroups.com

On Sep 15, 2011, at 4:46 PM, Brent Rowland wrote:

Here we go with bad dot notation. It looks like that should be good since you're only incrementing the right ivar, but it's not. You're actually incrementing the -right method call which can't be done. Another example of dot notation leading someone awry. ;)

Brad Miller
br...@cynicalpeak.com
http://cynicalpeak.com/

Brent Rowland

unread,
Sep 16, 2011, 1:21:54 AM9/16/11
to xcodep...@googlegroups.com
Sorry, I thought Jiva was a struct. My bad.

Brent

Sent from my iPhone

Jiva DeVoe

unread,
Sep 16, 2011, 1:30:48 AM9/16/11
to xcodep...@googlegroups.com, xcodep...@googlegroups.com
Interestingly, would this throw a compiler error? Or would it increment the pointer returned from the method call? Hmmm... ;)

--
Jiva DeVoe
Random Ideas, LLC
http://www.random-ideas.net

Reply all
Reply to author
Forward
0 new messages