An analysis of iOS development--KVO

Source: Internet
Author: User

Target: Listen to what's added to the Nsmutablearray object

The code is as follows:

C code
    1. -(void) Viewdidload
    2. {
    3. [Super Viewdidload];
    4. Self.dataarray = [Nsmutablearray arraywithobject:@"1"];
    5. [Self addobserver:self forkeypath:@"DataArray" options:nskeyvalueobservingoptionnew |  Nskeyvalueobservingoptionold Context:null];
    6. }
-(void) viewdidload{    [Super Viewdidload];    Self.dataarray = [Nsmutablearray arraywithobject:@ "1"];    [Self addobserver:self forkeypath:@ "DataArray" options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold context:null];     }

C code
    1. -(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: ( void *) Context
    2. {
    3. NSLog (@"%@", KeyPath);
    4. NSLog (@"%@", object);
    5. NSLog (@"%@", change);
    6. }
-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) context{    NSLog (@ "%@", keypath);    NSLog (@ "%@", object);    NSLog (@ "%@", change);}

C code
    1. -(Ibaction) add: (ID) sender
    2. {
    3. Nsarray *adddata = [Nsarray arraywithobjects:@"One", @ "a",@ "" ", Nil];
    4. [Self.dataarray Addobjectsfromarray:adddata];
    5. Self.dataarray = [Nsmutablearray arraywithobject:@"2"];
    6. }
-(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
  1. 2013-01-15 16:05:10.120 kvotest[2199:907] DataArray
  2. 2013-01-15 16:05:10.121 kvotest[2199:907] <ZZTViewController:0x20846590>
  3. 2013-01-15 16:05:10.123 kvotest[2199:907] {
  4. kind = 1;
  5. new = (
  6. 2
  7. );
  8. Old = (
  9. 1,
  10. 11,
  11. 12,
  12. 13
  13. );
  14. }
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
    1. -  (void) viewdidload  
    2. {  
    3.     [super viewdidload];  
    4.        
    5.     self.personObject = [PersonObject  personobjectwithbankinstance:[bankobject bankobjectwithaccountbalance:10]];  
    6.        
    7.     [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   
    8. }  
-(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
    1. -(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: ( void *) Context
    2. {
    3. NSLog (@"%@", KeyPath);
    4. NSLog (@"%@", object);
    5. NSLog (@"%@", change);
    6. }
-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) context{    NSLog (@ "%@", keypath);    NSLog (@ "%@", object);    NSLog (@ "%@", change);}

C code
    1. -(Ibaction) add: (ID) sender
    2. {
    3. [Self.personObject.bankInstance setaccountbalance:2111];
    4. }
-(Ibaction) add: (ID) sender{    [self.personObject.bankInstance setaccountbalance:2111];}

Output log:

C code
    1. 2013-01-15 16:05:10.111 kvotest[2199:907] Bankinstance.accountbalance
    2. 2013-01-15 16:05:10.116 kvotest[2199:907] <PersonObject:0x20856180>
    3. 2013-01-15 16:05:10.118 kvotest[2199:907] {
    4. kind = 1;
    5. new = 2111;
    6. Old = 10;
    7. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.