0

I'm now learning key value observing, have a very simple KVO project have a little problems that dont't print property new value changes when observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context method called. That's very weird, hope someone help me find where problems occurred, i'm very appreciated that!

Here is my source code:

ViewController.h

#import <UIKit/UIKit.h>

@class Person;

@interface ViewController : UIViewController

@property (nonatomic, strong) Person *person;

@end

ViewController.m

#import "ViewController.h"
#import "Person.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize person;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.person = [[Person alloc] init];
    [self changeName];
    [self.person addObserver:self
             forKeyPath:@"fullName"
                options:NSKeyValueObservingOptionNew
                context:NULL];
}

- (void)changeName
{
    self.person.fullName = @"Andy";
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if ([keyPath isEqualToString:@"fullName"])
    {
        NSLog(@"%@", change);
        NSString *string = [change objectForKey:NSKeyValueChangeNewKey];
        NSLog(@"%@", string);
    }
}

- (void)dealloc
{
    [self.person removeObserver:self forKeyPath:@"fullName"];
}

@end

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, strong) NSString *fullName;

@end

Person.m

#import "Person.h"

@implementation Person

@synthesize fullName = _fullName;

@end
Andy_24
  • 1,353
  • 2
  • 12
  • 20

2 Answers2

1

You're changing the name before you're adding the observer. Swap this:

[self changeName];
[self.person addObserver:self
         forKeyPath:@"fullName"
            options:NSKeyValueObservingOptionNew
            context:NULL];

...for this:

[self.person addObserver:self
         forKeyPath:@"fullName"
            options:NSKeyValueObservingOptionNew
            context:NULL];
[self changeName];
Jim
  • 72,985
  • 14
  • 101
  • 108
  • Now i want to register dependent keys which is "fullName" in class Person. i have created another class which is Person2, it have two properties is:@"firstName", @"lastName", and "fullName" value dependent either "firstName" and "lastName" changes. I try to implement it, but don't print any values in xcode console, i want to send you the source project, can you help me look into it? Thanks. – Andy_24 May 29 '13 at 11:45
  • 1
    If you have a new question, post it as a new question. – Jim May 29 '13 at 11:58
  • I just added a few lines code on this project test dependent keys, i send you project, can you help me look it? – Andy_24 May 29 '13 at 12:08
1

Have a look at the order of your calls,

You are calling changeName before you are registering as observer.

Move the changeName call below the addObserver method and see what happens.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • Now i want to register dependent keys which is "fullName" in class Person. i have created another class which is Person2, it have two properties is:@"firstName", @"lastName", and "fullName" value dependent either "firstName" and "lastName" changes. I try to implement it, but don't print any values in xcode console, i want to send you the source project, can you help me look into it? Thanks. – Andy_24 May 29 '13 at 11:49
  • @Andy_24 you want to implement `+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key` as explained in the [KVO Protocol Reference](http://developer.apple.com/library/iOS/#documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html#//apple_ref/occ/cat/NSKeyValueCoding). If you still don't understand, then ask another question. – Abizern May 29 '13 at 14:50