Target: Listen to what's added to the Nsmutablearray object
The code is as follows:
C code
- -(void) Viewdidload
- {
- [Super Viewdidload];
- Self.dataarray = [Nsmutablearray arraywithobject:@"1"];
- [Self addobserver:self forkeypath:@"DataArray" options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold Context:null];
- }
-(void) viewdidload{ [Super Viewdidload]; Self.dataarray = [Nsmutablearray arraywithobject:@ "1"]; [Self addobserver:self forkeypath:@ "DataArray" options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold context:null]; }
C code
- -(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: ( void *) Context
- {
- NSLog (@"%@", KeyPath);
- NSLog (@"%@", object);
- NSLog (@"%@", change);
- }
-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) context{ NSLog (@ "%@", keypath); NSLog (@ "%@", object); NSLog (@ "%@", change);}
C code
- -(Ibaction) add: (ID) sender
- {
- Nsarray *adddata = [Nsarray arraywithobjects:@"One", @ "a",@ "" ", Nil];
- [Self.dataarray Addobjectsfromarray:adddata];
- Self.dataarray = [Nsmutablearray arraywithobject:@"2"];
- }
-(Ibaction) add: (ID) sender{ nsarray *adddata = [Nsarray arraywithobjects:@ "one", @ "one", @ "" ", nil]; [Self.dataarray Addobjectsfromarray:adddata]; Self.dataarray = [Nsmutablearray arraywithobject:@ "2"];}
Enter the log:
C code
- 2013-01-15 16:05:10.120 kvotest[2199:907] DataArray
- 2013-01-15 16:05:10.121 kvotest[2199:907] <ZZTViewController:0x20846590>
- 2013-01-15 16:05:10.123 kvotest[2199:907] {
- kind = 1;
- new = (
- 2
- );
- Old = (
- 1,
- 11,
- 12,
- 13
- );
- }
2013-01-15 16:05:10.120 kvotest[2199:907] dataarray2013-01-15 16:05:10.121 kvotest[2199:907] <ZZTViewController: 0x20846590>2013-01-15 16:05:10.123 kvotest[2199:907] { kind = 1; New = ( 2 ); Old = ( 1, one, one, one );}
After testing the following conclusions: KVO monitoring is the change of the object pointer, nsstring, int, float and other objects (ABC = @ "123", ABC = 12, ABC = 12.2) are the pointer changes, so it is not feasible to capture the change of array in this way
However, we can do this way to change the properties of the control. As follows:
C code
- - (void) viewdidload
- {
- [super viewdidload];
-
- self.personObject = [PersonObject personobjectwithbankinstance:[bankobject bankobjectwithaccountbalance:10]];
-
- [self.personObject addObserver:self Forkeypath:@ "bankinstance.accountbalance" options:NSKeyValueObservingOptionNew | nskeyvalueobservingoptionold context:null]; // The note here is to listen for the accountbalance change of bankinstance under the Personobject object
- }
-(void) viewdidload{ [Super Viewdidload]; Self.personobject = [Personobject personobjectwithbankinstance:[bankobject bankobjectwithaccountbalance:10]]; [Self.personobject addobserver:self forkeypath:@ "bankinstance.accountbalance" options: Nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold Context:null]; Notice here is the accountbalance change of bankinstance under the Listener Personobject object}
C code
- -(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: ( void *) Context
- {
- NSLog (@"%@", KeyPath);
- NSLog (@"%@", object);
- NSLog (@"%@", change);
- }
-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) context{ NSLog (@ "%@", keypath); NSLog (@ "%@", object); NSLog (@ "%@", change);}
C code
- -(Ibaction) add: (ID) sender
- {
- [Self.personObject.bankInstance setaccountbalance:2111];
- }
-(Ibaction) add: (ID) sender{ [self.personObject.bankInstance setaccountbalance:2111];}
Output log:
C code
- 2013-01-15 16:05:10.111 kvotest[2199:907] Bankinstance.accountbalance
- 2013-01-15 16:05:10.116 kvotest[2199:907] <PersonObject:0x20856180>
- 2013-01-15 16:05:10.118 kvotest[2199:907] {
- kind = 1;
- new = 2111;
- Old = 10;
- }
2013-01-15 16:05:10.111 kvotest[2199:907] bankinstance.accountbalance2013-01-15 16:05:10.116 KVOTest[2199:907] < Personobject:0x20856180>2013-01-15 16:05:10.118 kvotest[2199:907] { kind = 1; new = 2111; Old = 10;}
If you have any questions, please comment together.
An analysis of iOS development--KVO