#import <Foundation/Foundation.h> @interface NameObServer : NSObject @end #import "NameObServer.h" @implementation NameObServer -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ NSLog(@"%@",keyPath); NSLog(@"%@",object); NSLog(@"%@",change); } @end ==================================== #import <Foundation/Foundation.h> @interface Student : NSObject @property(nonatomic,copy) NSString *name; @end #import "Student.h" @implementation Student -(void)dealloc{ [_name release]; [super dealloc]; } @end =================================== #import <Foundation/Foundation.h> #import "Student.h" #import "NameObServer.h" int main(int argc, const char * argv[]) { @autoreleasepool { Student *stu=[[[Student alloc] init] autorelease]; stu.name=@"mike"; NameObServer *nameobserve=[[[NameObServer alloc] init] autorelease]; //在给student的属性name添加观察对象 [stu addObserver:nameobserve forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil]; stu.name=@"jack"; //移除student的属性 name对象 [stu removeObserver:nameobserve forKeyPath:@"name"]; } return 0; }