欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ObjectC----Block用法以及用Block实现数组排序

程序员文章站 2022-09-28 13:56:37
// Created By 郭仔 2015年04月02日17:45:01   函数体外部分: typedef int (^Block)(int ,int );//重...

// Created By 郭仔 2015年04月02日17:45:01

 

函数体外部分:

typedef int (^Block)(int ,int );//重命名

typedef void (^Block1)(int );

int n = 0;

=================================================================================================================

// 定义函数指针

int (*p)(int ,int) = sum;

//通过函数指针调用函数

int re = p(10,3);

printf("%d\n",re);

// int n = 4;

 

Block block = ^(int x,int y){

// NSLog(@"%d",n);

return x+y;

};

NSLog(@"%d",block(1,1));

 

//定义block

int (^block1)(int,int) = ^(int x,int y){

return x+y;

}; //最后分号别忘了

 

//调用block

int result = block1(10,34);

printf("%d\n",result);

 

//两个练手例子

void (^block2)(int) = ^(int num){

printf("%d\n",num);

};

int (^block3)(int) = ^(int year){

return year;

};

block2(4);

printf("%d\n",block3(5));

 

int (^block4)(NSString *) = ^(NSString * str){

return [str intValue]; //对象方法intValue实现字符串转化为数值

};

NSLog(@"%d",block4(@"333"));

//printf("%d\n",block4(@"333"));

 

 

/*

block是匿名函数:因为调用函数有两种方式,一种是通过函数名调用,另一种是通过函数指针调用;

通过函数指针调用函数之前,需要给函数指针赋值,也需要用到函数名,所以对于匿名函数来说,这两种方式都不能实现函数的调用,因此使用block;

block和函数指针非常相似,但是有本质上的区别,但在使用方面,区别不大,我们可以将block当成函数指针,这样简化blcok的使用;

*/

__block int x = 10;

// int y;

Block1 block5 = ^(int num){

 

x++;

// y++;//局部变量

n ++; //全局变量

NSLog(@"%d",x);

NSLog(@"%d",num);

};

 

block5(5);

 

 

/*

1.在block里面能访问block之外的局部变量,不能修改block之外的局部变量,如果需要修改,该局部变量前面加__block;

2.在block中可以修改全局变量的值;

3.__block声明的变量会存储在内存的全局区(静态区)

*/

 

 

// =================================================================

 

Car * car1 = [Car carWithType:@"aa" andColor:@"bb" andPrice:134 andMaxSpeed:133 andCostOil:17];

 

Car * car2 = [Car carWithType:@"cc" andColor:@"dd" andPrice:234 andMaxSpeed:233 andCostOil:27];

 

Car * car3 = [Car carWithType:@"ee" andColor:@"ff" andPrice:334 andMaxSpeed:333 andCostOil:37];

 

Car * car4 = [Car carWithType:@"gg" andColor:@"hh" andPrice:434 andMaxSpeed:433 andCostOil:47];

 

NSMutableArray * cars = [NSMutableArray arrayWithObjects:car1,car2,car3,car4, nil];

 

//价格从高到低排序 block排序

//若用sortedArrayUsingComparator则需要重新定义一个数组保存(因为有返回值)

[cars sortUsingComparator:^NSComparisonResult(Car *obj1, Car *obj2) {

if ([obj1 price]<[obj2 price]) {

return NSOrderedDescending;

}

else if ([obj1 price] == [obj2 price])

return NSOrderedSame;

else

return NSOrderedAscending;

}];

 

for (Car *car in cars) {

NSLog(@"%@",car);

}

 

// maxSpeed从大到小排序,使用sortedUsingComparator

NSArray * arr = [cars sortedArrayUsingComparator:^NSComparisonResult(Car *obj1, Car *obj2) {

if ([obj1 maxSpeed]<[obj2 maxSpeed]) {

return NSOrderedDescending;

}

else if ([obj1 maxSpeed]==[obj2 maxSpeed])

return NSOrderedSame;

else

return NSOrderedAscending;

}];

 

for (Car * car in arr) {

NSLog(@"%@",car);

}

 

 

//自己定义block

NSComparisonResult (^block6)(Car *,Car*) = ^(Car *obj1,Car *obj2){

if ([obj1 maxSpeed]<[obj2 maxSpeed]) {

return NSOrderedDescending;

}

else if ([obj1 maxSpeed]==[obj2 maxSpeed])

return NSOrderedSame;

else

return NSOrderedAscending;

 

};

//然后调用sort方法,block6传参过去

[cars sortUsingComparator:block6];

 

for (Car * car in cars) {

NSLog(@"%@",car);

}

 

// ==============================================================

//字面量

//数组字面量

NSString * str = @"Literals";

//数组字面量

NSArray * arr1 = @[@"1",@"2",@"3"];

for (NSString * s in arr1) {

NSLog(@"%@",s);

}

//字典字面量

NSDictionary * dic = @{@"name":@"zhangsan",@"age":@"6"};

NSLog(@"%@,%@",dic[@"name"],dic[@"age"]);

 

 

NSNumber * num = @123;//把数字123转化成NSNumber对象

NSLog(@"%@",num);

// ===============================================================