0

From Apple's documentation I came across the following:

Setting values using key paths is not the same as setting them using Objective-C properties. You cannot use property notation to set transform values. You must use the setValue:forKeyPath: method with the preceding key path strings.

From my understanding, we must have properties of our ivars in order to use KVC.

But from the paragraph above, it seems to say otherwise:

Setting values using key paths is not the same as setting them using Objective-C properties. You cannot use property notation to set transform values.

Can someone explain to me why? Perhaps I am missing something, because all along my understanding is that we must have properties to utilize KVC.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • 1
    I think all it's saying is that this won't work: `setValue:value forKey:@"view.transform`. You'd have to use `setValue:value forKeyPath:@view.transform`. – Scott Berrevoets Mar 29 '14 at 23:06
  • @ScottBerrevoets You meant without the double quotes but keeping the **@** sign? – Unheilig Mar 29 '14 at 23:13
  • Sorry, typo on my part. I meant the `forKeyPath` part, as opposed to just `forKey`. – Scott Berrevoets Mar 30 '14 at 00:13
  • @ScottBerrevoets Sorry, didn't quite get you. You mean: `setValue:value forKeyPath:view.transform`? This wouldn't work. – Unheilig Mar 30 '14 at 00:16
  • No, it would be (without typos this time): `[self setValue:value forKeyPath:@"view.transform"]`. `view` is a property on `self`. – Scott Berrevoets Mar 30 '14 at 00:18
  • @ScottBerrevoets Aren't they both (i.e. `forKeyPath` and `forKey`) KVC compliant? Mind citing the difference? Perhaps I missed something here. – Unheilig Mar 30 '14 at 00:29
  • 1
    From https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html#//apple_ref/occ/instm/NSObject/setValue:forKeyPath: *keyPath A key path of the form relationship.property (with one or more relationships): for example "department.name" or "department.manager.lastName".* You can't do "department.name" in `setValue:forKey:`, you need `setValue:forKeyPath:` for that. – Scott Berrevoets Mar 30 '14 at 20:12
  • @ScottBerrevoets Will definitely read up. Thanks so much. – Unheilig Mar 30 '14 at 20:34

1 Answers1

1

From my understanding, we must have properties of our ivars in order to use KVC.

This is incorrect. KVC will use the property generated accessor methods, or other (appropriately named) accessor methods if they exist, but they aren't required. If they don't exist KVC will attempt to directly access the instance variables.

Check this Apple ref for a description of how KVC searches for the key to update.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thanks, but what would be a _"..other (appropriately named) accessor methods"_? Can you cite an example regarding this? – Unheilig Mar 29 '14 at 23:23
  • 1
    For setting only the set: method is actually valid, then instance variable names matching the pattern _, _is, , or is, in that order. – Wain Mar 30 '14 at 08:01