字符串分割
单个分割
NSString * str = @"adadhbdj,dfaslkdsafj,fdsakfasdkf,dddddd,eeeee,1234,2345,";
NSArray * arr = [str componentsSeparatedByString:@","];
NSLog(@"%@",arr);
分割一部分
NSString * str1 = @"adfsdabf12sadffdsaf12dsfa312sdfsdaf129dsaflsda12000";
NSArray * arr1 = [str1 componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"129"]];
NSLog(@"%@",arr1);
会出现"",所以需要通过循环判断str[i]==0来过滤掉"";
排序
NSArray *arr2 = @[@"7",@"9",@"1",@"5",@"4",@"10",@"2",@"3",@"6"];
NSMutableArray *arr3 = [NSMutableArray arrayWithArray:arr2];
SEL mySel = @selector(compare:);
一个字符一个字符的比较,所以结果为1,10,2,3,4…
NSArray * array = [arr3 sortedArrayUsingSelector:mycelia];
NSLog(@"%@",array);
[arr3 sortUsingSelector:mySel];
介绍Block
//不带参 不带返回值
void (^ myBlock1)(void) =^{
NSLog(@"myBlock1 Test...");
};
//调用
myBlock1();
//不带参 带返回值
int (^ myBlock2)() =^{
return 10;
};
int ret = myBlock2();
NSLog(@"ret is %d",ret);
//带参 带返回值
int (^ myBlock3)(int a,int b,int c) =^(int a,int b,int c){
return a+b+c;
};
int ret1 =myBlock3(10,20,30);
NSLog(@"ret1 is %d",ret1);
//__block,
__block int sum = 10;
void (^test)(void) =^(void){
sum -=50;
};
test();
NSLog(@"sum is %d",sum);
使用block排序
[arr3 sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//告诉block obj1与obj2比较结果;
//实现比较规则,该block会被多次调用
if([obj1 integerValue] <[obj2 integerValue]){
return NSOrderedDescending;
}
else if ([obj1 integerValue] ==[obj2 integerValue]){
return NSOrderedSame;
}else{
return NSOrderedAscending;
}
}];
NSLog(@"%@",arr3);