Search code examples
objective-ccocoa-touchkey-value-observingkey-value-coding

Using -setValue:forKey: vs "object.var = ..."


The difference between these two lines of code is that the second is KVO compliant and the first isn't?

[person setValue:tempPerson.name forKey:@"name"];
person.name = tempPerson.name;

The reason I'm asking is because I need to update 60 attributes on over 500 objects, I don't want KVO notifications for more than a handful of attributes.

I'm using NSFecthedResultsController for my UITableView and I don't want to trigger excessive setNeedDisplay on the cells.


Solution

  • There is no difference between the two lines as far as Key-Value Observing is concerned. Both trigger KVO notifications by default. You can override this behavior, though. From Apples' KVO Programming Guide:

    You can control automatic observer notifications for properties of your subclass by implementing the class method automaticallyNotifiesObserversForKey:. Subclasses can test the key passed as the parameter and return YES if automatic notification should be enabled, NO if it should be disabled.