ios學習筆記之block在ios開發中的應用,ios學習筆記

來源:互聯網
上載者:User

ios學習筆記之block在ios開發中的應用,ios學習筆記

一、什麼是Blocks 
     Block是一個C層級的文法以及運行時的一個特性,和標準C中的函數(函數指標)類似,但是其運行需要編譯器和運行時支援,從ios4.0開始就很好的支援Block。 
二、在ios開發中,什麼情況下使用Block 
     Block除了能夠定義參數列表、傳回型別外,還能夠擷取被定義時的詞法範圍內的狀態(比如局部變數),並且在一定條件下(比如使用__block變數)能夠修改這些狀態。此外,這些可修改的狀態在相同詞法範圍內的多個block之間是共用的,即便出了該詞法範圍(比如棧展開,出了範圍),仍可以繼續共用或者修改這些狀態。通常來說,block都是一些簡短程式碼片段的封裝,適用作工作單元,通常用來做並發任務、遍曆、以及回調。 
三、block如何申明(對比於c語言中的函數申明) 
四、c函數指正和blocks調用 
     int (*CFunc) (int a) 函數調用 
     int result = CFunc(10); 
     int (^BFunc)  (int  a)  函數調用 
     int result = BFunc(10); 
五、__block  關鍵字 
     一個Block的內部時可以引用自身範圍外的變數的,包括static變數,extern變數或自由變數(定義一個變數的時候,如果不加儲存修飾符,預設情況下就是自由變數auto,auto變數儲存在stack中的。除了auto之外還存在register,static等儲存修飾符),對於自由變數,在Block中唯讀。在引入block的同時,還引入了一種特殊的__block關鍵字變數儲存修飾符。 
六、block的幾個小例子 

  • #import <Cocoa/Cocoa.h>  
  •   
  •   
  • int main(int argc, char *argv[])  
  • {  
  •     @autoreleasepool {  
  •         NSLog(@"Hello world");  
  •         void (^myblocks) (void) = NULL;  
  •         myblocks = ^(void) {  
  •             NSLog(@"in blocks");  
  •         };  
  •         NSLog(@"before myblocks");  
  •         myblocks();  
  •         NSLog(@"after myblocks");  
  •           
  •           
  •         int (^myblocks2) (int a, int b) = ^(int a, int b) {  
  •             int c = a + b;  
  •             return c;  
  •         };  
  •         NSLog(@"before blocks2");  
  •         int ret = myblocks2(10, 20);  
  •         NSLog(@"after blocks2 ret %d", ret);  
  •           
  •         //此處如果不加__block會報錯  
  •         __blockint sum = 0;  
  •         int (^myblocks3) (int a, int b) = ^(int a, int b) {  
  •             sum = a + b;  
  •             return3;  
  •         };  
  •         myblocks3(20, 30);  
  •         NSLog(@"sum is %d", sum);  
  •     }  
  •     returnNSApplicationMain(argc, (constchar **)argv);  
  • }  

  • 列印結果如下 
    2012-09-03 10:23:20.878 blockTest[407:403] Hello world 
    2012-09-03 10:23:20.880 blockTest[407:403] before myblocks 
    2012-09-03 10:23:20.881 blockTest[407:403] in blocks 
    2012-09-03 10:23:20.881 blockTest[407:403] after myblocks 
    2012-09-03 10:23:20.882 blockTest[407:403] before blocks2 
    2012-09-03 10:23:20.882 blockTest[407:403] after blocks2 ret 30 
    2012-09-03 10:23:20.882 blockTest[407:403] sum is 50 
    七、block寫的回調例子 
    1、Dog.h 

    Java代碼   
    1. #import <Foundation/Foundation.h>  
    2.   
    3.   
    4. @interface Dog : NSObject {  
    5.     int _ID;  
    6.     NSTimer *timer;  
    7.     int barkCount;  
    8.       
    9.     //定義一個blocks變數  
    10.     void (^BarkCallback) (Dog *thisDog, int count);  
    11. }  
    12. @property (assign) int ID;  
    13.   
    14.   
    15. //向外暴露一個介面  
    16. -(void) setBark:( void (^) (Dog *thisDog, int count) ) eachBark;  
    17.   
    18.   
    19. @end  


    2、Dog.m 

    Java代碼   
    1. #import "Dog.h"  
    2.   
    3.   
    4. @implementation Dog  
    5. @synthesize ID = _ID;  
    6.   
    7.   
    8. -(id) init  
    9. {  
    10.     self = [superinit];  
    11.     if (self) {  
    12.         //每隔1s調用一次updateTimer方法  
    13.         timer = [NSTimerscheduledTimerWithTimeInterval:1.0ftarget:selfselector:@selector(updateTimer:) userInfo:nilrepeats:YES];  
    14.           
    15.     }  
    16.     returnself;  
    17. }  
    18.   
    19.   
    20. -(void) updateTimer:(id) arg  
    21. {  
    22.     barkCount ++;  
    23.     NSLog(@"dog %d bark count %d", _ID, barkCount);  
    24.     //向Person對象進行彙報  
    25.     if (BarkCallback) {  
    26.         //調用從Person傳過來的Blocks  
    27.         BarkCallback(self, barkCount);  
    28.     }  
    29. }  
    30.   
    31.   
    32.   
    33.   
    34. -(void) setBark:(void (^)(Dog *, int))eachBark  
    35. {  
    36.     [BarkCallbackrelease];  
    37.     BarkCallback = [eachBark copy];  
    38. }  
    39.   
    40.   
    41. -(void) dealloc  
    42. {  
    43.     [BarkCallbackrelease];  
    44.     [superdealloc];  
    45. }  
    46. @end  


    3、Person.h 

    Java代碼   
    1. #import <Foundation/Foundation.h>  
    2. #import "Dog.h"  
    3.   
    4.   
    5. @interface Person : NSObject  
    6. {  
    7.     Dog *_dog;  
    8. }  
    9.   
    10.   
    11. @property (retain) Dog *dog;  
    12.   
    13.   
    14. @end  


    4、Person.m 

  • #import "Person.h"  
  •   
  •   
  • @implementation Person  
  • @synthesize dog = _dog;  
  •   
  •   
  • -(void) setDog:(Dog *)dog  
  • {  
  •     if (_dog != dog) {  
  •         [_dogrelease];  
  •         _dog = [dog retain];  
  •           
  •         [_dogsetBark:^(Dog *thisDog, int count) {  
  •             NSLog(@"person dog %d count %d", [thisDog ID], count);  
  •         }];  
  •     }  
  • }  
  •   
  •   
  • -(Dog *) dog  
  • {  
  •     return_dog;  
  • }  
  •   
  •   
  • -(void) dealloc  
  • {  
  •     self.dog = nil;  
  •     [superdealloc];  
  • }  
  •   
  •   
  • @end  

  • 5、Main.m 

    Java代碼   
    1. #import <Foundation/Foundation.h>  
    2. #import "Person.h"  
    3. #import "Dog.h"  
    4.   
    5.   
    6. int main(int argc, constchar * argv[])  
    7. {  
    8.   
    9.   
    10.     @autoreleasepool {  
    11.           
    12.         // insert code here...  
    13.         NSLog(@"Hello, World!");  
    14.         Person *person = [[Personalloc] init];  
    15.         Dog *dog = [[Dogalloc] init];  
    16.         [dog setID:10];  
    17.         [person setDog:dog];  
    18.         [dog release];  
    19.         while (1) {  
    20.             [[NSRunLoopcurrentRunLoop] run];  
    21.         }  
    22.         [person release];  
    23.           
    24.     }  
    25.     return 0;  
    26. }  


    6、列印結果 
    2012-09-03 11:21:08.551 blockDelegate[549:403] Hello, World! 
    2012-09-03 11:21:09.554 blockDelegate[549:403] dog 10 bark count 1 
    2012-09-03 11:21:09.555 blockDelegate[549:403] person dog 10 count 1 
    2012-09-03 11:21:10.554 blockDelegate[549:403] dog 10 bark count 2 
    2012-09-03 11:21:10.555 blockDelegate[549:403] person dog 10 count 2 
    2012-09-03 11:21:11.553 blockDelegate[549:403] dog 10 bark count 3 
    2012-09-03 11:21:11.554 blockDelegate[549:403] person dog 10 count 3 
    2012-09-03 11:21:12.554 blockDelegate[549:403] dog 10 bark count 4 
    2012-09-03 11:21:12.555 blockDelegate[549:403] person dog 10 count 4 
    2012-09-03 11:21:13.553 blockDelegate[549:403] dog 10 bark count 5 
    2012-09-03 11:21:13.553 blockDelegate[549:403] person dog 10 count 5 
    2012-09-03 11:21:14.553 blockDelegate[549:403] dog 10 bark count 6 
    2012-09-03 11:21:14.554 blockDelegate[549:403] person dog 10 count 6

    相關文章

    聯繫我們

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