Declaring and Using a Block(定义和使用Block)
1— 无参的block
- // 1 无参
- void (^myblocks) (void) = NULL;//声明
- myblocks = ^(void){
- NSLog(@"myblocks");
- };// 给myblcok 赋值
- myblocks(); //调用
2— 带参的block
// 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
In many cases, you don’t need to declare block variables;(大部分情况下,你无需声明一个block 变量)
本例子:将数组逆序
- char *myCharacters[3] = { "TomJohn", "George", "Charles Condomine" };
- qsort_b(myCharacters, 3, sizeof(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" }
- 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 | NSForcedOrderingSearch;
- NSLocale *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 修饰临时变量,可让其变为全局的变量。(引用自身作用域外的变量)
__block 参数全局声明
- __block int sum = 0;
- int (^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
- */
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。