1.block的四种形式
无参无返回值:
void (^b1)() = ^(){// 定义跟函数指针的形式类似
NSLog(@"hello!!!");}; b1();
无参有返回值:
int(^b2)() = ^(){ return 1; };// 花括号外面有分号
NSLog(@"%d",b2());
有参无返回值:
void (^b3)(int,int) = ^(int a,int b){
NSLog(@"%d",a+b); }; b3(3,5);
有参有返回值:
int (^b4)(int ,int ) = ^(int a,int b){
return a > b ? a : b; }; NSLog(@"%d",b4(3,4));
2.block的typedef的两种形式
1⃣️:
typedef int (^BB) (int,int);
BB mb1 = ^(int a,int b){
return a+b; };
NSLog(@"%d",mb1(3,5));
2⃣️:
typedef int (BBB)(int,int);
BBB ^mb2 = ^(int a,int b){
return a > b ? a : b;
}; NSLog(@"%d",mb2(3,5));
3.block和变量
1⃣️:
void (^b1)(int ) = ^(int i){// 形参
i = 10;// 形参只是拷贝了实参的值,本身的值不会改变
NSLog(@"%d",i);
}; b1(5);// 实参 并没有重复定义变量
2⃣️:
static int i = 10;// 用static修饰之后,变量放在静态区
// __block int i = 10;// --全局变量(也可以放在主函数外)
void(^b2)() = ^(){
i = i+2; NSLog(@"%d",i); }; b2();
//如果想要在block中使用局部变量i
// 1局部变量可以被访问
// 2局部变量不可以再次被赋值(仅仅是做了一个拷贝)如果想赋值必须使用__block关键字修饰
4.block和数组排序
1⃣️:
NSArray *arr1 = [NSArray arrayWithObjects:
@"abcb",@"abca",@"abcd",@"abcc", nil];
// 这是系统typedef了一个block 给他赋值要用block形式 用来排序
NSComparator sort = ^(NSString *s1,NSString *s2){
return [s1 compare:s2]; };
NSLog(@"%@",[arr1 sortedArrayUsingComparator:sort]);
2⃣️:
NSArray *arr2 = [arr1 sortedArrayUsingComparator:
^NSComparisonResult(NSString * obj1, NSString * obj2) {
// if ([obj1 compare:obj2] == NSOrderedDescending) {
// return NSOrderedDescending;
// }else if ([obj1 compare:obj2] == 0){
// return NSOrderedSame;
// }
// else return NSOrderedAscending; }]; NSLog(@"%@",arr2);
附加:
代码1:
// 返回值是一个数组,参数也是个数组 实现数组的排序
1⃣️:
NSMutableArray * (^b6)(NSMutableArray *) = ^(NSMutableArray *arr){
for (int i = 0; i < [arr count] - 1; i++) {
for (int j = 0; j < [arr count] - 1 - i; j++) {
if ([arr[j]compare:arr[j + 1]] == NSOrderedAscending) {
[arr exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
return arr;
for (int i = 0; i < [arr count] - 1; i++) {
for (int j = 0; j < [arr count] - 1 - i; j++) {
if ([arr[j]compare:arr[j + 1]] == NSOrderedAscending) {
[arr exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
return arr;
};
NSMutableArray *mArr = [NSMutableArray arrayWithObjects:@"aaa",@"bbb",@"ccc", nil];
NSLog(@"%@",b6(mArr));
2⃣️:
NSMutableArray *(^sort)(NSMutableArray *) = ^(NSMutableArray *arr){
[arr sortUsingSelector:@selector(compare:)];
// [arr sortedArrayUsingSelector:@selector(compare:)];// 不可变数组
// 系统自带了一个方法 名字叫compare: 默认为升序
return arr; };
NSMutableArray *marr = [NSMutableArray arrayWithObjects:@"12",@"15",@"13", nil]; NSLog(@"%@",sort(marr));
代码2:
- (NSString *)description{
return [NSString stringWithFormat:@"name:%@ age:%ld",self.name,self.age];
return [NSString stringWithFormat:@"name:%@ age:%ld",self.name,self.age];
}
Person *p1 = [[Person alloc]initWithName:@"kobi" age:18];
Person *p2 = [[Person alloc]initWithName:@"james" age:15];
Person *p3 = [[Person alloc]initWithName:@"durant" age:20];
NSArray *Arr = [NSArray arrayWithObjects:p1,p2,p3, nil];
使用block 分别用年龄和姓名对数组进行排序
NSArray *arr3 = [Arr sortedArrayUsingComparator:^NSComparisonResult(Person * obj1, Person * obj2) {
if ([obj1.name compare:obj2.name] == 1) {
return NSOrderedDescending;
}else if ([obj1.name compare:obj2.name] == -1){
return NSOrderedAscending;
}
else
return NSOrderedSame;
}];
NSArray *arr4 = [Arr sortedArrayUsingComparator:^NSComparisonResult(Person* obj1, Person* obj2) {
if (obj1.age > obj2.age) {
return NSOrderedDescending;
}else if (obj1.age < obj2.age){
return NSOrderedAscending;
}
else
return NSOrderedSame;
}];
NSLog(@"%@",arr3);
if ([obj1.name compare:obj2.name] == 1) {
return NSOrderedDescending;
}else if ([obj1.name compare:obj2.name] == -1){
return NSOrderedAscending;
}
else
return NSOrderedSame;
}];
NSArray *arr4 = [Arr sortedArrayUsingComparator:^NSComparisonResult(Person* obj1, Person* obj2) {
if (obj1.age > obj2.age) {
return NSOrderedDescending;
}else if (obj1.age < obj2.age){
return NSOrderedAscending;
}
else
return NSOrderedSame;
}];
NSLog(@"%@",arr3);
NSLog(@"%@",arr4);