Array Accessors

妙处在于:如果iVar是数组,元素支持KVC,那么可以通过 valueForKey:key  得到一个数组结果集 (如果都有这个key)


Key–value coding allows an object to implement a key as if its value were an array (or a set), even if it isn’t. This is similar to what I said earlier about how accessors function as a façade, putting an instance variable name in front of hidden complexities. To illustrate, I’ll add these methods to the class of our object myObject:

- (NSUInteger) countOfPepBoys {

    return [self.theData count];

}


- (id) objectInPepBoysAtIndex: (NSUInteger) ix {

    return (self.theData)[ix];

}

By implementing countOf... and objectIn...AtIndex:, I’m telling the key–value coding system to act as if the given key (@"pepBoys" in this case) existed and were an array. An attempt to fetch the value of the key @"pepBoys" by way of key–value coding will succeed, and will return an object that can be treated as an array, though in fact it is a proxy object (an NSKeyValueArray). Thus we can now say [myObject valueForKey: @"pepBoys"] to obtain this array proxy, and we can say [myObject valueForKeyPath: @"pepBoys.name"] to get the same array of strings as before.

(This particular example may seem a little silly because the underlying implementation is already an array instance variable, but you can imagine an implementation whereby the result of objectInPepBoysAtIndex: is obtained through some completely different sort of operation.)

The proxy object returned through this sort of façade behaves like an NSArray, not like an NSMutableArray. If you want the caller to be able to manipulate the proxy object provided by a KVC façade as if it were a mutable array, you must implement two more methods, and the caller must obtain a different proxy object by calling mutableArrayValueForKey:. So, for example (we are now presuming that theData is a mutable array):

- (void) insertObject: (id) val inPepBoysAtIndex: (NSUInteger) ix {

    [self.theData insertObject:val atIndex:ix];

}


- (void) removeObjectFromPepBoysAtIndex: (NSUInteger) ix {

    [self.theData removeObjectAtIndex: ix];

}

Now it is possible to call [myObject mutableArrayValueForKey: @"pepBoys"] to obtain something that acts like a mutable array. (The true usefulness of mutableArrayValueForKey: will be clearer when we talk about key–value observing inChapter 13.)

A complication for the programmer is that none of these methods can be looked up directly in the documentation, because they involve key names that are specific to your object. You can’t find out from the documentation what removeObjectFromPepBoysAtIndex: is for; you have to know, in some other way, that it is part of the implementation of key–value coding compliance for a key @"pepBoys" that can be obtained as a mutable array. Be sure to comment your code so that you’ll be able to understand it later.

Another complication, of course, is that getting a method name wrong can cause your objectnot to be key–value coding compliant. Figuring out why things aren’t working as expected in a case like that can be tricky.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值