0

I've looked at some other SO answers in regards to this and I thought I was implementing my code correctly but I am not getting results.

I have a mutable array property - arrLocations. In my .m file, in viewDidLoad I set up an observer for it and then add an item:

    self.arrLocations = [[NSMutableArray alloc] init];

    //add an observer to know when geocoding loops are updated
    [self addObserver:self forKeyPath:@"arrLocations" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

    [self insertObject:@"test" inArrLocationsAtIndex:0];

and then I have the KVO method:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{

    if([keyPath isEqualToString:@"arrLocations"]) {

        NSLog(@"Showing contents of self.arrLocations\n%@", self.arrLocations);

    }

}

But the observer method never gets called.

PruitIgoe
  • 6,166
  • 16
  • 70
  • 137

1 Answers1

0

The observer never gets called because the pointer to your array stays the same when you change the contents of your array.

You would have to add an observer to the array itself and observe a key of the array. Something like count. But you cannot do that because NSMutableArray is not KVO compliant.

So, to make this work you have to find another way. My first idea would be to create a wrapper class for NSMutableArray that fires a notification each time you add or remove items to your array.

joern
  • 27,354
  • 7
  • 90
  • 105