티스토리 뷰
Key-Value Coding Programming Guide - Key-Value Coding Fundamentals - Accessing Collection Properties
rhinoPHS 2019. 1. 31. 18:33Key-Value Coding Programming Guide - Key-Value Coding Fundamentals - Accessing Collection Properties
KVC를 사용하는 객체에 일대다 프로퍼티는 다른 프로퍼티와 마찬가지로 set이나 get을 사용할 수 있습니다.
key나 keypath와 관련된 set, get 메소드를 사용하면됩니다. 그리고 콜렉션 요소를 다루고 싶을 수 도 있습니다.그럴 때는 NSKeyValueCoding에 있는 proxy메소드를 사용하는 게 좋습니다
이 프로토콜에는 콜렉션 객체에 접근하는 3가지 다른 proxy 메소드가 있습니다. 각각은 key, keypath로 나눠집니다.
mutableArrayValueForKey:
andmutableArrayValueForKeyPath:
These return a proxy object that behaves like an
NSMutableArray
object.mutableSetValueForKey:
andmutableSetValueForKeyPath:
These return a proxy object that behaves like an
NSMutableSet
object.mutableOrderedSetValueForKey:
andmutableOrderedSetValueForKeyPath:
These return a proxy object that behaves like an
NSMutableOrderedSet
object.
프록시 객체에서 객체를 더하거나 제거하거나 변경할 때, 기본 구현은 기본프로퍼티를 적절히 수정합니다.
이건 valueForKey로 non-mutable 콜렉션 객체를 받아와서 수정본을 만들고 다시 setValue:forKey:로 저장하는 것보다 효율적입니다. 그리고 많은 경우에는 mutable property를 직접 다루는 것보다 효율적입니다. 그리고 KVO할 때도 콜렉션 객체에 있던 객체가 KVO를 따르는데 도움을 줍니다.