//
Main.m
Block jobs
//
Created by lanou3g on 14-11-20.
Copyright (c) 2014 Lanou_zhou. All rights reserved.
//
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
Definition? A block with a return value of bool, with two nsstring parameters. Implementation: Sentencing
The broken strings are equal.
BOOL (^comparestring) (NSString *, nsstring *) = ^ (NSString *str1, NSString *str2) {
if ([str1 isequaltostring:str2]) {
return YES;
}else{
return NO;
}
};
if (CompareString (@ "SSS", @ "SSS")) {
NSLog (@ "Same");
}
Definition? A block, the return value is Nsinteger, there are two parameters,? One is Nsarray, one is nsstring. Implementation: The array is judged to contain this string, if included, returns the subscript of the string, if not included, returns-1.
Nsarray *arr = @[@ "AA", @ "ss", @ "WW"];
NSString *str = @ "ss";
Int (^find) (Nsarray *, nsstring *) = ^ (Nsarray *arr, NSString *str) {
for (int i = 0; i < Arr.count; i++) {
if ([Arr[i] isequaltostring:str]) {
return i;
}
}
return-1;
};
int f = Find (ARR,STR);
NSLog (@ "%d", f);
Create an array that is initialized to @ "123", @ "21", @ "33", @ "69", @ "108",
@ "256". To use the block syntax, to sort the array of rows in. and output the content. Sorted results: 108 123 21 256 33 69
Reference: Sortedarraywithoptions:usingcomparator:
Non-variable groups
Nsarray *arr1 = @[@ "23", @ "1", @ "3", @ "2", @ "108"];
Nsarray *ARR2 = [arr1 sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {
if ([Obj1 intvalue] > [obj2 intvalue]) {
return nsordereddescending;
}
return nsorderedsame;
}
];
NSLog (@ "sortarray1:%@", arr2);
Nsarray *arr11 = @[@ "23", @ "1", @ "3", @ "2", @ "108"];
Nsarray *a1 = [Arr11 sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {
if ([Obj1 intvalue] > [obj2 intvalue]) {
return nsordereddescending;
}
return nsorderedsame;
}
];
NSLog (@ "sortarray1:%@", A1);
5, block for variable array sorting
5,1
Nscomparator Sortblock = ^ (ID string1, id string2)
{
return [string1 compare:string2];
};
Nsmutablearray *stringarray = [Nsmutablearray arraywithobjects:@ "G", @ "C", @ "B", @ "a", nil];
Nsarray *a = [Stringarray sortedarrayusingcomparator:sortblock];
NSLog (@ "array:%@", a);
Block immutable groups written separately
Nscomparator SortBlock1 = ^ (ID string1, id string2)
{
return [string1 compare:string2];
};
Nsarray *stringarray2 = [Nsarray arraywithobjects:@ "G", @ "C", @ "B", @ "a", nil];
Nsarray *a2 = [StringArray2 sortedarrayusingcomparator:sortblock1];
NSLog (@ "block variable array written separately:%@", A2);
return 0;
}
Block array sorting