Block 语法基础

发布时间:2020-07-14 13:27:20 作者:sanminx
来源:网络 阅读:397

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 语法基础

截至到目前位置,都先声明了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 语法基础

__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. */ 

 

推荐阅读:
  1. 网站访问慢延迟高排查
  2. 高质量C++读书笔记(复习用)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

语法 block objc

上一篇:until__shell脚本

下一篇:golang语言渐入佳境[27]-其他类型转string函数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》