Block syntax, block usage, and array sorting by block in OC

Source: Internet
Author: User

Block : A syntax block, essentially an anonymous function (a function without a name)There is no post-extended version of Block,c language in standard C, with anonymous functions addedin C + +, JS, Swift and other languages have similar syntax, called closuresThe block syntax is similar to the function pointers in C, so let's review the functions and function pointers in C languageThe C language function refers to the code snippet that implements a functionThe complete function consists of two parts: function declaration, function definitionfunction declaration, which is the function prototype. For example:int sum (int x,int y), with two shaping parameters, a function to reshape the return valuefunction definition, which is the function implementation. For example:int sum (int x,int y) { return x + y;}function pointer (variable): A pointer variable that holds the address of a function (the name of the letter)Int (*p) (int x,int) = sum;function pointer type:INT (*) (int x,int y) that is: points to two shaping parameters, a pointer to an integer function that returns valuesfunction pointer variable:p value of function pointer:sum Block anonymous function: function with no nameFor example:int (int x,int y) because block is an anonymous function, the implementation of a block variable holds functions that can be directly called by the block variableblock type:int (^) (int) block variable:myblock block Value:^int (int num) {return 7*num;} that is: ^ return value type (parameter list) {function Body} where return value type can be omittedFor example, write a block with a return value of an OC string (only one parameter), implementing the ability to convert the string to shaping Int (^myblock) (NSString *) = ^ (NSString *string) { return [string intvalue];    };
NSLog (@ "%d", Myblock (@ "123"));The data type of the block represents the format of the anonymous function (return value type, type of formal parameter)the definition of a block variable is similar to the definition of a function pointer variable, and the only difference from the function pointer variable is the variable name, which is modified by the caret (^) .you should first use the ^ modifier, the remainder is consistent with the C language function definition, the biggest difference is that there is no function name (and the return value type can also be omitted)The block variable is defined with the basic characteristics of the variable definition, and the anonymous function on the right of the assignment number can be assigned as a whole, similar to int a = 5;The value assigned to the block variable is an anonymous function. It is also a feature of the function, and it is the only one that can be defined within a function implementation (the C language considers that a function cannot be nested, and block is a special case)block for typedeftypedef int (^blocktype) (int x,int y)Primitive Type:int (^) (int x,int y) new type:blocktype The type definition of the analogy function pointer, the format is consistent with the function pointer, the type definition simplifies the use of block to some extent. typedef int (^sumtype) (int,int);
Sumtype sumblock = ^ (int x,int y) {
return x + y;
    };
int result = Sumblock (5,4);
NSLog (@ "%d", result); The __block type identifies a local variable that can run normal access within its subsequent defined block, __block int num = 0;

void (^testblock) () = ^ () {
for (int i = 0; i < i++) {
count++;
num++;
NSLog (@ "%d", count);
        }    };Block CallTestblock ();  array using block sorting arrays are used to compare two strings using a blockNscomparisonresult (^compareblock) (nsstring *,nsstring *) = ^ (NSString *str1,nsstring *str2) {return [str2 compare:str1];//If it is ascending return-1, if it is descending returns 1, if equal returns 0    };NSLog (@ "%ld", Compareblock (@ "one" @ "one"));  Nsarray *array = @[@1,@2,@13,@12,@23];
Nsarray *resultarray1 = [Array sortedarrayusingselector: @selector (compare:)];
NSLog (@ "%@", resultArray1); arrays are sorted in descending order using block sort  Nscomparator Sortblock = ^ (id obj1,id obj2) { return [Obj2 compare:obj1];
    };
Nsarray *resultarray2 = [array sortedarrayusingcomparator:sortblock];
NSLog (@ "%@", resultArray2); Ascending order ArrangementNsarray *resultarray3 = [array sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {return [obj1 compare:obj2];    }];NSLog (@ "%@", resultArray3);   

Block syntax, block usage, and array sorting by block in OC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.