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