您好,登錄后才能下訂單哦!
Declaring and Using a Block(定義和使用Block)
1— 無參的block
- // 1 無參
- void (^myblocks) (void) = NULL;//聲明
- myblocks = ^(void){
- NSLog(@"myblocks");
- };// 給myblcok 賦值
- myblocks(); //調(diào)用
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 變量)
本例子:將數(shù)組逆序
- 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 作為參數(shù)) typically either to perform an operation on a collection of objects, or to use as a callback after an operation has finished.(通常要么執(zhí)行操作在對象集合、或者操作回調(diào))下面簡單的例子展示了如何在NSArray 的方法 sortedArrayUsingComparator: 中使用block,該方法將block作為一個參數(shù)。為了解釋說明,本例子的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 修飾臨時變量,可讓其變?yōu)槿值淖兞俊#ㄒ米陨碜饔糜蛲獾淖兞浚?/p>
__block 參數(shù)全局聲明
- __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
- */
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。