KVO - 观察NSArray中包含的对象的属性

时间:2016-07-26 11:04:46

标签: ios objective-c key-value-observing

仍然无法使用NSArray对象消化KVO。我的要求是例如假设在车库里有多辆车。我想观察汽车轮胎性能的变化,如前轮胎升级或后轮胎升级。

Car.h

@property(nonatomic, strong) NSString *frontTyre;
@property(nonatomic, strong) NSString *backTyre;

Garage.h

@property(nonatomic, strong) NSArray *cars; //Think there are two cars in the garage.

在CarOwner.m

[Garage addObserver:self forKeyPath:@"cars" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];]


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"cars"])
    {
       //Here I am able to get the car object which got a type upgrade.
       //But how to know which tyre got upgraded? 
    }
}

升级汽车轮胎时我正在做:

[self willChange:NSKeyValueChangeSetting valuesAtIndexes:index forKey:@"remoteUsers"];
[car1.frontTyre = @"upgraded"];
[self didChange:NSKeyValueChangeSetting valuesAtIndexes:index forKey:@"remoteUsers"];

我应该this还是this方式?或者您可能有任何其他建议。

2 个答案:

答案 0 :(得分:1)

您可以使用添加观察者,如下所示:

[Garage.cars addObserver:self toObjectsAtIndexes:[NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, Garage.cars.count)] forKeyPath:@"frontTyre" options:0 context:nil];

请务必在必要时删除观察者。

答案 1 :(得分:0)

在阅读了更多有关KVO的内容后,我们发现无法观察到可变数组中包含的对象的属性。我们可以通过(如@zylenv提到的)

来观察不可变对象数组的属性
<div class="records" >
  <div class="btlrow">
      <div class="cell">#</div>
      <div class="cell">Movie</div>
      <div class="cell">Distributor</div>
      <div class="cell">Country</div>
      <div class="cell">Language</div>
      <div class="cell">Name</div>       
      <div class="cell">Role</div>  
      <div class="cell">Born</div>      
      <div class="cell">Star Sign</div>
      <div class="cell">Episodes</div> 
  </div>
  <div  class="btlrow">
      <div class="cell">145</div>
      <div class="cell">Game Of Thrones</div>
      <div class="cell">HBO</div>
      <div class="cell">USA</div>    
      <div class="cell">English</div>
      <div class="cell"><a href="http://www.imdb.com/name/nm0227759/?ref_=tt_cl_t1">Peter Dinklage</a></div>
      <div class="cell">Tyrion Lannister</div>
      <div class="cell">June 11, 1969</div>       
      <div class="cell">Gemini</div>  
      <div class="cell">2-3-4-6-11</div>  
  </div>  
  <div class="btlrow">
      <div class="cell">146</div>
      <div class="cell">Game Of Thrones</div>
      <div class="cell">HBO</div>
      <div class="cell">USA</div>    
      <div class="cell">English</div>
      <div class="cell"><a href="http://www.imdb.com/name/nm3229685/?ref_=tt_cl_t4">Kit Harington</a></div>
      <div class="cell">Jon Snow</div>
      <div class="cell">December 26, 1986</div>       
      <div class="cell">Capricorn</div>  
      <div class="cell">1-2-3-6-9</div>  
  </div>
</div>

但是,要观察对象属性的可变数组,请执行以下步骤:

  1. 将观察者添加到数组中以获得有关对象插入和删除操作的通知。
  2. 收到插入通知后,将观察者添加到感兴趣的新添加对象的属性
  3. 当收到删除通知时,只需从步骤#2中添加的对象中删除观察者(注意:删除的对象将位于更改词典的“旧”键中)