Object—C 塊在函數中作為參數時的分析

來源:互聯網
上載者:User

標籤:blog   http   io   ar   檔案   sp   on   c   log   

暫時對這個有了一些粗淺的理解,記下來一邊後面學習時學習,改正。

先舉個例子:

A類:

.h檔案:

@interface A  : NSObject

- (void)Paly1:(void (^)(double salary))game1

           Paly2:(void (^)(double salary))game2;

 

A類:

.m檔案:

- (void)Paly1:(void (^)(double salary))game1

           Paly2:(void (^)(double salary))game2{

           double zsalary;

           scanf("%lf",&zsalary);

           if(zsalary>5000){

            Play1(zsalary);

          }else{

           Play2(zsalary);

          }

}

@end;

B 類:

.h檔案:

@interface B :NSObject 

- (void)Play;

@end;

 

.m 檔案:

 

@implementation B

- (void)Play{

           A *a = [A new];

           [a Play1:^(double salary)game1{

           NSLog(" 超過5000了,玩game1去!");

           } Play2:^(doule salary)game2{

           NSLog(" 靠,還沒有5000,只能玩game2了!");
           }];

}

@end;

 

通過Block可以實現回調,也常說成委託。當程式運行時,雖然B類的函數調用類A類的含Block的函數,但B類並沒有真正的權利去調用這個Block,只能調用A,真正由權利去調用Blocks的是A類定義的函數,這是通過這個函數在A類中定義的語句來判斷具體進入哪個Block中。通過這個就是回調,委託。

具體的詳細解說參見(牛人部落格):http://www.cnblogs.com/ygm900/p/3633131.html

 

Object—C 塊在函數中作為參數時的分析

聯繫我們

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