块(block)
block, 块语法,C语言函数的扩展, 允许在函数内部定义一个block, block执行效率比较高
block的语法(与函数指针的语法非常相似)
返回值类型 (^block名)(参数类型1参数名, 参数类型2参数名2, ...) = ^(参数类型1参数名, 参数类型2参数名2, ...){
功能代码
};
注: 没有参数, 括号不能省略
block变量
数据类型变量名 = 初值
void (^blockName)() = ^() { NSLog(@"Hello, 你好"); }; blockName();
写block的思路
1.写成函数怎么写
2.对照写出block()(输入inlineBlock, 会自动提示)
写一个block, "求一个数平方"
int (^doubleNumber)(int number) = ^(int number){ return number * number; }; NSLog(@"%d", doubleNumber(3));
写一个block,求两个数的最大值
int(^blockMax)(int, int) = ^(int a, int b) { return a > b ? a : b; }; NSLog(@"max = %d", blockMax(3, 4));
写一个返回值为整型参数为NSString(仅一个参数)的block,实现将字符串转换为整型的功能。
NSInteger(^changeToInt)(NSString *string) = ^(NSString *string) { return [string integerValue]; }; NSLog(@"%ld", changeToInt(@"12a3"));
写一个block, 求一个字母对应的ACSII值
//1 int(^changeASCII1)(char) = ^(char c) { int a = c; return a; }; NSLog(@"%d", changeASCII1('a')); //2 void(^changeASCII)(char) = ^(char a) { NSLog(@"%d", a); }; changeASCII('a');
写一个block, 求三个数的中间值
int(^middleValue)(int, int, int) = ^(int a, int b, int c) { //1 //return a > b ? (b > c ? b : (a > c ? c : a)) : (a > c ? a : (b > c ? c : b)) ; //2 return a + b + c - MAX(MAX(a, b), c) - MIN(MIN(a, b), c); }; NSLog(@"middle = %d", middleValue(3, 4, 5));
block的使用
1.使用typedef对block类型重命名
typedef int (^BlockType)(int, int); BlockType sum = ^(int a, int b){ return a + b; }; NSLog(@"%d", sum(2, 3));
2.block内部访问变量
block内部不能够修改局部变量的值
如果在block内部修改变量的值, 可以
1.全局变量或静态变量
2.用__block修饰局部变量
写一个block, 打印输出一个整数
// static int num = 1024; __block int num = 1024; void(^printNumber)() = ^() { NSLog(@"%d", num); num++; }; printNumber(); printNumber(); printNumber();
数组排序
NSArray *array = [NSArray arrayWithObjects:@"30", @"12", @"4", @"100" , @"1000", nil];
1.提供相邻元素的比较方法
缺点: 只能升序排序
适用范围: 对姓名按照a->z排序
NSArray *sortArray = [array sortedArrayUsingSelector:@selector(compare:)]; NSLog(@"%@", sortArray);
2.提供相邻元素的比较规则
//写法1 //比较规则 block NSComparisonResult(^rule)(NSString *, NSString *) = ^(NSString *str1, NSString *str2) { //满足NSOrderedAscending的条件, 决定了排序的结果 if ([str1 intValue] > [str2 intValue]) { return NSOrderedDescending; } else if ([str1 intValue] == [str2 intValue]) { return NSOrderedSame; } else { return NSOrderedAscending; } }; NSArray *sortArray1 = [array sortedArrayUsingComparator:rule]; NSLog(@"%@", sortArray1); //写法2 NSArray *sortArray2 = [array sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) { if ([obj1 intValue] < [obj2 intValue]) { return NSOrderedDescending; } else if ([obj1 intValue] == [obj2 intValue]) { return NSOrderedSame; } else { return NSOrderedAscending; } }]; NSLog(@"%@", sortArray2);
Student *stu1 = [[Student alloc]initWithName:@"chenqiang" number:2 score:59.9]; Student *stu2 = [[Student alloc]initWithName:@"bengbeng" number:28 score:40]; Student *stu3 = [[Student alloc]initWithName:@"gexing" number:41 score:60]; Student *stu4 = [[Student alloc]initWithName:@"yanglong" number:3 score:70]; Student *stu5 = [[Student alloc]initWithName:@"yalong" number:61 score:61]; NSArray *studentArray = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4, stu5, nil]; //分数由高到低 NSArray *sortByscore = [studentArray sortedArrayUsingComparator:^NSComparisonResult(Student *stu1 , Student *stu2) { if ([stu1 score] < [stu2 score]) { return NSOrderedDescending; } else if ([stu1 score] == [stu2 score]) { return NSOrderedSame; } else { return NSOrderedAscending; } }]; NSLog(@"%@", stu1);//类名+地址 for (Student *stu in sortByscore) { [stu showStudents]; } NSLog(@" ***********************华丽的分割线********************************* "); //姓名由a-z排序 NSArray *sortByname = [studentArray sortedArrayUsingComparator:^NSComparisonResult(Student *stu1 , Student *stu2) { // if ([[stu1 name] compare: [stu2 name]] == 1) { // return NSOrderedDescending; // } else if ([[stu1 name] compare: [stu2 name]] == 0) { // return NSOrderedSame; // } else { // return NSOrderedAscending; // } // NSComparisonResult result = [[stu1 name] compare:[stu2 name]]; return [[stu1 name] compare:[stu2 name]]; }]; for (Student *stu in sortByname) { [stu showStudents]; }
语法糖(syntactic sugar)
1.数值(比如100 3.14 YES)前加@, 转化成NSNumber
NSNumber *number1 = [NSNumber numberWithInt:100]; NSNumber *number2 = @100; NSNumber *number3 = @3.14; NSNumber *number4 = @YES; NSArray *numberArray = [NSArray arrayWithObjects:@100, @3.14, @YES, nil];
2.@""快速创建一个字符串
NSString *string1 = [[NSString alloc] initWithString:@"abc"]; NSString *string2 = @"abc";
3.@[]快速创建一个NSArray对象
NSArray *array1 = [NSArray arrayWithObjects:@"a", @"b", @"c", nil]; NSArray *array2 = @[@"a", @"b", @"c"];
4.数组名[下标], 访问数组中的某个元素
NSLog(@"%@", [array1 objectAtIndex:1]); NSLog(@"%@", array2[1]);
5.@{}快速创建一个NSDictionary的对象
NSDictionary *dic1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"xiaoming", @"name", @"男", @"gender", @"18", @"age", nil]; NSDictionary *dic2 = @{@"name":@"xiaoming", @"gender":@"男", @"age":@"18"};
6.字典名[key], 取key对应的value值
NSLog(@"%@", [dic1 objectForKey:@"name"]); NSLog(@"%@", dic2[@"name"]);