Block 語法基礎

Declaring and Using a Block(定義和使用Block)

1— 無參的block

  1. // 1 無參 
  2.         void (^myblocks) (void) = NULL;//聲明 
  3.         myblocks = ^(void){ 
  4.             NSLog(@"myblocks"); 
  5.         };// 給myblcok 賦值 
  6. myblocks(); //調用

2— 帶參的block

 

  1. int multiplier = 7;  
  2. int (^myBlock)(int) = ^(int num) {  
  3.     return num * multiplier;  
  4. }; 
  5. printf("%d",myBlock(3));
  6. 
    // prints "21"

 

 

3—— 有返回值,帶參block

 

  1. int (^myblocks2) (int a, int b) = ^(int a ,int b){ 
  2.             int c = a+b; 
  3.             return c; 
  4.         }; 
  5. int ret = myblocks2(2,3); 
  6.  // prints  "5"

 

 

 

截至到目前位置,都先聲明瞭block,再對block 賦值!其實你完全可以直接使用一個block

 

Using a Block Directly

 

In many cases, you don’t need to declare block variables;(大部分情況下,你無需聲明一個block 變量)

本例子:將數組逆序

  1. char *myCharacters[3] = { "TomJohn", "George", "Charles Condomine" }; 
  2.  
  3. qsort_b(myCharacters, 3, sizeof(char *), ^(const void *l, const void *r) { 
  4.     char *left = *(char **)l; 
  5.     char *right = *(char **)r; 
  6.     return strncmp(left, right, 1); 
  7. }); 
  8.  
  9. // myCharacters is now { "Charles Condomine", "George", "TomJohn" } 

Blocks with Cocoa

 

  1. Several methods in the Cocoa frameworks take a block as an argument, typically 
  2. either to perform an operation on a collection of objects, or to use as a callback
  3.  after an operation has finished. The following example shows how to use a block with
  4.  the NSArray method sortedArrayUsingComparator:. The method takes a single argument—the
  5.  block. For illustration, in this case the block is defined as an NSComparator local 
  6. variable: 

 take a block as an argument,(將block 作爲參數) typically either to perform an operation on a collection of objects, or to use as a callback after an operation has finished.(通常要麼執行操作在對象集合、或者操作回調)下面簡單的例子展示瞭如何在NSArray 的方法 sortedArrayUsingComparator: 中使用block,該方法將block作爲一個參數。爲了解釋說明,本例子的block被定義爲一個NSComparator 的本地變量。

本例子:NSArray 排序

  1. NSArray *stringsArray = [NSArray arrayWithObjects: 
  2.                                  @"string 1", 
  3.                                  @"String 21", 
  4.                                  @"string 12", 
  5.                                  @"String 11", 
  6.                                  @"String 02", nil]; 
  7.   
  8. static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch | 
  9.         NSWidthInsensitiveSearch | NSForcedOrderingSearch; 
  10. NSLocale *currentLocale = [NSLocale currentLocale]; 
  11.   
  12. NSComparator finderSortBlock = ^(id string1, id string2) { 
  13.   
  14.     NSRange string1Range = NSMakeRange(0, [string1 length]); 
  15.     return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale]; 
  16. }; 
  17.   
  18. NSArray *finderSortArray = [stringsArray sortedArrayUsingComparator:finderSortBlock]; 
  19. NSLog(@"finderSortArray: %@", finderSortArray); 
  20.   
  21. /* 
  22. Output: 
  23. finderSortArray: ( 
  24.     "string 1", 
  25.     "String 02", 
  26.     "String 11", 
  27.     "string 12", 
  28.     "String 21" 
  29. */ 

__block Variables(__block 變量(關鍵字))

__block 修飾臨時變量,可讓其變爲全局的變量。(引用自身作用域外的變量)

__block 參數全局聲明

  1. __block int sum = 0
  2. int (^myblock3) (int a,int b) = ^(int a ,int b){ 
  3.     sum = a+b; 
  4.     return sum; 
  5. }; 
  6. ret = myblock3(2,3); 
  7. NSLog(@"block3 %d",ret); 
  1. NSArray *stringsArray = [NSArray arrayWithObjects: 
  2.                          @"string 1", 
  3.                          @"String 21", // <- 
  4.                          @"string 12", 
  5.                          @"String 11", 
  6.                          @"Strîng 21", // <- 
  7.                          @"Striñg 21", // <- 
  8.                          @"String 02", nil]; 
  9.   
  10. NSLocale *currentLocale = [NSLocale currentLocale]; 
  11. __block NSUInteger orderedSameCount = 0
  12.   
  13. NSArray *diacriticInsensitiveSortArray = [stringsArray sortedArrayUsingComparator:^(id string1, id string2) { 
  14.   
  15.     NSRange string1Range = NSMakeRange(0, [string1 length]); 
  16.     NSComparisonResult comparisonResult = [string1 compare:string2 options:NSDiacriticInsensitiveSearch range:string1Range locale:currentLocale]; 
  17.   
  18.     if (comparisonResult == NSOrderedSame) { 
  19.         orderedSameCount++; 
  20.     } 
  21.     return comparisonResult; 
  22. }]; 
  23.   
  24. NSLog(@"diacriticInsensitiveSortArray: %@", diacriticInsensitiveSortArray); 
  25. NSLog(@"orderedSameCount: %d", orderedSameCount); 
  26.   
  27. /* 
  28. Output: 
  29.   
  30. diacriticInsensitiveSortArray: ( 
  31.     "String 02", 
  32.     "string 1", 
  33.     "String 11", 
  34.     "string 12", 
  35.     "String 21", 
  36.     "Str\U00eeng 21", 
  37.     "Stri\U00f1g 21" 
  38. orderedSameCount: 2 
  39. */ 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章