Block in iOS

Source: Internet
Author: User

Define a block:

Int (^blockname) (int, int);

Blockname = ^ (int a, int b) {

Return a>b? A:B;

};

This block is equivalent to:

Int (^blockname) (int, int) = ^ (int a, int b) {

Return a>b?a:b;

};

Use:

Nsinterge a = Blockname (3, 4);

NSLog (@ "A=%ld", a);

. Block as a parameter of a method
Nsarray *namearray = [Nsarray arraywithobjects:@ "Zhao", @ "Qian", @ "Sun", @ "Li", nil];
Nsarray *arr = [NameArray sortedarrayusingselector: @selector (compare:)];
NSLog (@ "%@", arr);

Nsmutablearray *numberarray = [Nsmutablearray arraywithobjects:@ "2", @ "1", @ "ten", @ "015", nil];
Nsarray *sortarray1 = [Numberarray sortedarrayusingselector: @selector (compare:)];
NSLog (@ "%@", sortArray1);

Nsarray *ARR2 = [Numberarray sortedarrayusingcomparator:^nscomparisonresult (NSString *obj1, NSString *obj2) {
Rules for comparison of adjacent two elements (Obj1, OBJ2)
if ([Obj1 intvalue] > [obj2 intvalue]) {
Comparison rules for adjacent two elements (Obj1, OBJ2)
System sequencing to meet nsoorderdescending conditions for swapping
Note: Quickly determine the well-taken array to meet the conditions of the nsorderascending

return nsorderedascending;
}else if ([obj1 intvalue] = = [Obj2 intvalue]) {
return nsorderedsame;
}else{
return nsordereddescending;
}
}];

Block in iOS

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.