2

I have 2 class, Foo and Bar, as below:

@interface Foo : NSObject
   @property Bar *bar;
@end

@interface Bar: NSObject
   @property int intVal;
   @property long longVal;
@end

Now I want to use KVO to observe for change in property bar of an object of class Foo. But I would like to know if I change 1 of bar's properties, intVal or longVal, is there any notification sent to the observer?

it4rb
  • 166
  • 1
  • 9

1 Answers1

5

You can observe value changes for bar using KVO from Foo like this:

[bar addObserver:self
           forKeyPath:@"intVal"
              options:NSKeyValueObservingOptionNew
              context:nil];

[bar addObserver:self
           forKeyPath:@"longVal"
              options:NSKeyValueObservingOptionNew
              context:nil];

And then you need to implement observeValueForKey method like following:

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

    if ([keyPath isEqualToString:@"longVal"]) {
    // property longVal changed
    }
    ...
}

EDIT:

Something that has been pointed out is the correctness of the answer, so I'll try to make it more complete:

declare a global variable in Foo like this:

static void * const MyClassKVOContext = (void*)&MyClassKVOContext;

and pass it as context when you add the observer:

[bar addObserver:self
           forKeyPath:@"longVal"
              options:NSKeyValueObservingOptionNew
              context:MyClassKVOContext];

To finish, when you observe:

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

        if (context != MyClassKVOContext) {
    [super observeValueForKeyPath:keyPath
                         ofObject:object
                           change:change
                          context:context]; return; }

    if ([keyPath isEqualToString:@"longVal"]) {
    // property longVal changed
    }
    ...
}

This will guarantee the maximum of uniqueness possible for the observation context. For a more complete explanation please refer to the linked answer in the comments, as I still think is out of the scope of this topic.

Danny S
  • 1,291
  • 8
  • 12
  • See [this answer](http://stackoverflow.com/a/14162363/104790) for how to do KVO correctly. – Nikolai Ruhe Oct 24 '14 at 12:43
  • My answer was intended to describe the initial approach on how to observe value changes and how he could figure out whether it was one property or the other. It wasn't a lecture on KVO. – Danny S Oct 24 '14 at 13:05
  • This is correct. Nikolai has just pointed to a more confusing unnecessary response. – latenitecoder Oct 24 '14 at 13:05
  • @DanSpag The question asks for a lecture on KVO. -1 for introducing a beginner to a known-to-be-inferior pattern. – Nikolai Ruhe Oct 24 '14 at 13:30
  • 1
    @NikolaiRuhe So you should be given a +1 for introducing a beginner to a pattern that he couldn't understand because he misses the basics. – Danny S Oct 24 '14 at 13:37
  • I added the comment to make **you** update your answer. If you feel the OP can't possibly understand the linked answer on her own just add the two lines that make your answer correct. – Nikolai Ruhe Oct 24 '14 at 14:15
  • @NikolaiRuhe I edited the answer to add the context discussion as pointed out in the answer you linked. This add more layers to the answer that IMHO wasn't incomplete or wrong, but just essential to answer the question. So I'm still convinced that the down vote and the affirmation that my answer is wrong have no place. – Danny S Oct 24 '14 at 15:15