oc 文法 kvo,ockvo

來源:互聯網
上載者:User

oc 文法 kvo,ockvo

點名主題 :

kvo就是一個類來監聽另外一個類的變數,當變數發生改變的時候來通知這個類。

舉個例子:

建立一個人的類,人的類中有一個銀行卡;建立一個銀行卡的類,銀行卡的類中有一個錢的變數;

我們來用人的類來監聽銀行卡中的錢數,當銀行卡中的錢數發生改變的時候來通知人;

下面我們來實現這個例子;

建立一個


建立一個person類

person.h

#import <Foundation/Foundation.h>@class BankCard;@interface Person : NSObject{    BankCard *bancard;//銀行卡}@end

person.m

#import "Person.h"#import "BankCard.h"@implementation Person//重寫初始化函數-(id) init{    self = [super init];    if(self)    {        bancard = [[BankCard alloc]init];        //添加一個監聽 監聽的變數為 bancard 中得money的變數  舊的值和新的值        [bancard addObserver:self forKeyPath:@"money" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];    }    return self;}//當監聽的值發生改變的時候自動調用該方法-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{    if([keyPath isEqual:@"money"])    {        NSString *new = [change objectForKey:@"old"];        NSString *old =[change objectForKey:@"new"];        NSLog(@"%@   %@",new,old);    }}@end

銀行卡類:

BankCard.h

#import <Foundation/Foundation.h>@interface BankCard : NSObject@property (nonatomic,assign) float money;@end
BankCard.m

#import "BankCard.h"@implementation BankCard-(id)init{    self = [super init];    if(self)    {        _money=10.0f;        //這裡用了定時器,看不懂沒關係,知道意思就行,就是每隔1秒調用一下balanceUpdate這個函數來改變money的值,以便我們來觀察是否監聽成功;        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(balanceUpdate) userInfo:nil repeats:YES];        }    return self;}-(void) balanceUpdate{    float f = self.money;    f += arc4random()%100;//產生一個0-99 的隨機數    self.money = f;}@end

main.m

#import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * argv[]){    @autoreleasepool {                // insert code here...        NSLog(@"Hello, World!");        Person *p = [[Person alloc]init];        [[NSRunLoop currentRunLoop]run];    }    return 0;}


運行結果:

2014-07-25 21:51:32.210 IOS KVO[3388:303] Hello, World!2014-07-25 21:51:33.214 IOS KVO[3388:303] 10   932014-07-25 21:51:34.213 IOS KVO[3388:303] 93   1542014-07-25 21:51:35.214 IOS KVO[3388:303] 154   1822014-07-25 21:51:36.213 IOS KVO[3388:303] 182   2142014-07-25 21:51:37.213 IOS KVO[3388:303] 214   3072014-07-25 21:51:38.213 IOS KVO[3388:303] 307   3582014-07-25 21:51:39.213 IOS KVO[3388:303] 358   4042014-07-25 21:51:40.213 IOS KVO[3388:303] 404   4932014-07-25 21:51:41.213 IOS KVO[3388:303] 493   542Program ended with exit code: -1

參考自千鋒視頻。


SAT OC答案解析!!!(要文法,閱讀,數學都有的)

懶得減壓一個個上傳了,這裡面有OG和OC的,解壓之後OC的檔案夾裡會有oc的題目和答案解析

 
iOS開發中,OC文法規範要的先後順序是什?

self.label.text = 意思就是自身的這個類的一個label控制項對象的text欄位是多少,

self本類定義 包含有UIlabel執行個體對象 ,UIlabel類有text這個屬性。
你說的text.self.labe=@"hello world"? 中text就是最基本的屬性了,再怎麼分呢。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.