문제

I am trying to get my head wrapped around "key-value coding".

Apple's docs say:

This document describes the NSKeyValueCoding informal protocol, which defines a mechanism allowing applications to access the properties of an object indirectly by name (or key), rather than directly through invocation of an accessor method or as instance variables.

Few thing are confusing me

  1. Accessor methods are automatically generated for properties, and provide several benefits like memory management, custom validations, etc. When we access a property without the accessor methods as the Apple doc says, does that mean we are losing the benefits of accessor methods?

  2. If KVC is so good and it reduces code, why should I use accessor methods, or why are accessor methods still around?

  3. I have never seen in any tutorial or books, or code on GitHub using KVC. Why is it not adopted that widely?

도움이 되었습니까?

해결책

  1. No, it just means that you aren't explicitly calling the accessor in your code. The accessor is called for you by the KVC implementation.

  2. KVC doesn't necessarily reduce code, rather it allows a different way of interacting. It allows more runtime flexibility and it can allow the use of key paths. You shouldn't look at it as a full replacement, just as an alternative which is appropriate in some cases.

  3. It is used widely, you need to look for calls to valueForKey:, setValue:forKey: (the methods of the protocol - there are many more than just these couple).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top