views:

323

answers:

2

Put simply, is there a way to receive a general notification when any property in an Objective-C class is changed? I know I can use KVO to monitor particular property changes, but I have the need to call a particular method whenever any setProperty: message is sent to my class. I want to be able to receive a generic notification without any concern about which property in particular was modified.

If it helps to clarify why I want to do this, I am making use of some fast table scrolling code found here: http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/

Part of the process of accomplishing this is that whenever a property in a table view cell is modified, [ self setNeedsDisplay ] needs to be called. I'd rather not have to override the setter methods for every property in my class just to make this call.

A: 

Not exactly. You can create a dependent key that depends on every property you wish to expose and then observe that. That's about as close as you'll get, I think.

Chuck
Hm. Seems like I would be abusing the concept of dependent keys in this case. I suppose I'll just observe each property individually. I could have sworn that there was a generic way to do this. How about a way to catch the name of the message being passed to a class via the runtime?
LucasTizma
Not really. Just call the dependent key `propertiesChanged` or something like that and it will be quite accurate. mmalc at Apple actually suggests this technique in his Cocoa Bindings Example and Hints: http://homepage.mac.com/mmalc/CocoaExamples/controllers.html … As for "catching the name of the message being passed to a class", I assume you mean catching *all* messages sent to a class. This would require either a proxy object or hooking into the `objc_msgSend()` runtime function, the latter of which would be massively slow and hackish and really not worth it.
Chuck
+2  A: 

As Chuck notes, you can create a dependent key, or of course you can directly observe all the properties (which is less work than overloading the setters).

Using the Objective-C runtime, if you exclusively use properties, you can automate this process using class_copyPropertyList(). But I'd probably only do this if this problem comes up a bit for you. If you only have one instance of this problem, it's probably easier and safer and more maintainable just to directly observe the list of properties unless you feel like working in the ObjC runtime.

Rob Napier