Ios development-KVO analysis, ios-kvo Analysis

Source: Internet
Author: User

Ios development-KVO analysis, ios-kvo Analysis

Target: Listen to what is 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: @ "11", @ "12", @ "13", nil];
  • [Self. dataArray addObjectsFromArray: addData];
  • Self. dataArray = [NSMutableArray arrayWithObject: @ "2"];
  • }
  • - (IBAction)add:(id)sender{    NSArray *addData = [NSArray arrayWithObjects:@"11", @"12", @"13", nil];    [self.dataArray addObjectsFromArray:addData];        self.dataArray = [NSMutableArray arrayWithObject:@"2"];}

     

    Input log:

    C code
  • 16:05:10. 120 KVOTest [2199: 907] dataArray
  • 16:05:10. 121 KVOTest [2199: 907] <ZZTViewController: 0x20846590>
  • 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,        11,        12,        13    );}

     

     

    The test result shows that kvo listens for changes in object pointers, NSString, int, float, and other objects (abc = @ "123", abc = 12, abc = 12.2) all are pointer changes, so it is not feasible to capture array changes.

     

    However, we can use this method to change the control attributes. As follows:

    C code
  • -(Void) viewDidLoad
  • {
  • [Super viewDidLoad];
  • Self. personObject = [PersonObject personObjectWithBankInstance: [BankObject bankObjectWithAccountBalance: 10];
  • [Self. personObject addObserver: self forKeyPath: @ "bankInstance. accountBalance" options: NSKeyValueObservingOptionNew | condition context: NULL]; // you can view the change in the accountBalance of bankInstance under the personObject.
  • }
  • -(Void) viewDidLoad {[super viewDidLoad]; self. personObject = [PersonObject personObjectWithBankInstance: [BankObject bankObjectWithAccountBalance: 10]; [self. personObject addObserver: self forKeyPath: @ "bankInstance. accountBalance "options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context: NULL]; // you can check the change in the accountBalance of bankInstance under the 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
  • 16:05:10. 111 KVOTest [2199: 907] bankInstance. accountBalance
  • 16:05:10. 116 KVOTest [2199: 907] <PersonObject: 0x20856180>
  • 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 leave a message for further discussion.

    Related Article

    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.