温馨提示×

温馨提示×

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

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

Block 语法基础

发布时间:2020-07-14 13:27:20 阅读:437 作者:sanminx 栏目:开发技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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

1— 无参的block

// 1 无参         void (^myblocks) (void) = NULL;//声明         myblocks = ^(void){             NSLog(@"myblocks");         };// 给myblcok 赋值 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

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

Block 语法基础

Block 语法基础

 Block 语法基础

截至到目前位置,都先声明了block,再对block 赋值!其实你完全可以直接使用一个block

Using a Block Directly

In many cases, you don’t need to declare block variables;(大部分情况下,你无需声明一个block 变量)

本例子:将数组逆序

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

Blocks with Cocoa

Several methods in the Cocoa frameworks take a block as an argument, typically either to perform an operation on a collection of objects, or to use as a callback after an operation has finished. The following example shows how to use a block with the NSArray method sortedArrayUsingComparator:. The method takes a single argument—the block. For illustration, in this case the block is defined as an NSComparator local 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 排序

NSArray *stringsArray = [NSArray arrayWithObjects:                                  @"string 1",                                  @"String 21",                                  @"string 12",                                  @"String 11",                                  @"String 02"nil];   static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch |         NSWidthInsensitiveSearch | NSForcedOrderingSearchNSLocale *currentLocale = [NSLocale currentLocale];   NSComparator finderSortBlock = ^(id string1, id string2) {       NSRange string1Range = NSMakeRange(0, [string1 length]);     return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale]; };   NSArray *finderSortArray = [stringsArray sortedArrayUsingComparator:finderSortBlock]; NSLog(@"finderSortArray: %@", finderSortArray);   /* Output: finderSortArray: (     "string 1",     "String 02",     "String 11",     "string 12",     "String 21" ) */ 

__block Variables(__block 变量(关键字))

__block 修饰临时变量,可让其变为全局的变量。(引用自身作用域外的变量)

Block 语法基础

__block 参数全局声明

__block int sum = 0int (^myblock3) (int a,int b) = ^(int a ,int b){     sum = a+b;     return sum; }; ret = myblock3(2,3); NSLog(@"block3 %d",ret); 
NSArray *stringsArray = [NSArray arrayWithObjects:                          @"string 1",                          @"String 21", // <-                          @"string 12",                          @"String 11",                          @"Strîng 21", // <-                          @"Striñg 21", // <-                          @"String 02", nil];   NSLocale *currentLocale = [NSLocale currentLocale]; __block NSUInteger orderedSameCount = 0;   NSArray *diacriticInsensitiveSortArray = [stringsArray sortedArrayUsingComparator:^(id string1, id string2) {       NSRange string1Range = NSMakeRange(0, [string1 length]);     NSComparisonResult comparisonResult = [string1 compare:string2 options:NSDiacriticInsensitiveSearch range:string1Range locale:currentLocale];       if (comparisonResult == NSOrderedSame) {         orderedSameCount++;     }     return comparisonResult; }];   NSLog(@"diacriticInsensitiveSortArray: %@", diacriticInsensitiveSortArray); NSLog(@"orderedSameCount: %d", orderedSameCount);   /* Output:   diacriticInsensitiveSortArray: (     "String 02",     "string 1",     "String 11",     "string 12",     "String 21",     "Str\U00eeng 21",     "Stri\U00f1g 21" ) orderedSameCount: 2 */ 

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

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

AI

开发者交流群×