ios開發--KVO淺析,ios--kvo淺析

來源:互聯網
上載者:User

ios開發--KVO淺析,ios--kvo淺析

目標:監聽NSMutableArray對象中增加了什麼

 

代碼如下:

C代碼  
  • - (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代碼  
  • - (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代碼  
  • - (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"];}

     

    輸入日誌:

    C代碼  
  • 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,        11,        12,        13    );}

     

     

    經過測試得如下結論:kvo監聽的是對象指標的變動,NSString、int、float等對象的變動(abc = @"123"、abc = 12、abc = 12.2)皆為指標的變動,所以通過此方式來捕捉array的變化是不可行的

     

    但,我們可以通過此方式來做控制項屬性的變動。如下:

    C代碼  
  • - (void)viewDidLoad  
  • {  
  •     [super viewDidLoad];  
  •       
  •     self.personObject = [PersonObject personObjectWithBankInstance:[BankObject bankObjectWithAccountBalance:10]];  
  •       
  •     [self.personObject addObserver:self forKeyPath:@"bankInstance.accountBalance" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];    // 此處注意是監聽personObject對象下的bankInstance的accountBalance變化  
  • }  
  • - (void)viewDidLoad{    [super viewDidLoad];        self.personObject = [PersonObject personObjectWithBankInstance:[BankObject bankObjectWithAccountBalance:10]];        [self.personObject addObserver:self forKeyPath:@"bankInstance.accountBalance" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];    // 此處注意是監聽personObject對象下的bankInstance的accountBalance變化}

     

    C代碼  
  • - (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代碼  
  • - (IBAction)add:(id)sender  
  • {  
  •     [self.personObject.bankInstance setAccountBalance:2111];  
  • }  
  • - (IBAction)add:(id)sender{    [self.personObject.bankInstance setAccountBalance:2111];}

     輸出日誌:

    C代碼  
  • 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;}

     

    如有問題,請留言共同探討。

    相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.