1) create a console output project:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HLQ9-0.png "style =" float: none; "title =" QQ20130828-13.png "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HM628-1.png "style =" float: none; "title =" QQ20130828-14.png "/>
2) Add two new classes: BankObject and PersonObject. Note that the parent class is NSObject.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HI431-2.png "style =" float: none; "title =" QQ20130828-15.png "/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HH211-3.png "style =" float: none; "title =" QQ20130828-16.png "/>
3) Add the following attributes to the BankObject header file and source file:
@property int accountBalance;
@synthesize accountBalance;
4) Add a response function to the PersonObject source file.
- (void)ob
The following prompt is displayed. Click Enter.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HKO6-4.png "style =" float: none; "title =" QQ20130828-17.png "/>
5) as a Demo, We can output the modified value in this function. The NSLog statement added in this function:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HJT2-5.png "style =" float: none; "title =" QQ20130828-18.png "/>
6) switch to the main. m file, add the following code, and introduce the header file.
#import "BankObject.h"#import "PersonObject.h"
Comment out the original output statement and add the following code:
BankObject * bankInstance = [[BankObject alloc] init]; PersonObject * personInstance = [[PersonObject alloc] init]; /** @ brief register the personInstance as the observer of the accountBalance attribute */[bankInstance addObserver: personInstance forKeyPath: @ "accountBalance" options: NSKeyValueObservingOptionNew context: NULL]; bankInstance. accountBalance = 100; bankInstance. accountBalance = 200; bankInstance. account balance = 300;
7) switch to the output of the PersonObject. m source file and place the breakpoint. When you run the program, the program jumps to the breakpoint,
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HK607-6.png "style =" float: none; "title =" QQ20130828-21.png "/>
8) Click 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HI637-7.png "title =" QQ20130828-23.png "/> to step down, the new value of the output is 100.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104HG004-8.png "style =" float: none; "title =" QQ20130828-22.png "/>
Reference connection: http://www.cocoadev.cn/Objective-C/Key-Value-Observing-Quick-Start-cn.asp
This article from the "Jiangshan wind and rain" blog, please be sure to keep this source http://965678322.blog.51cto.com/4935622/1284103