#import <Foundation/Foundation.h>
//1.取中间值
int middle(int a, int b, int c)
{
int max = a > b ? a : b;
max = max > c ? max : c;
int min = a < b ? a : b;
min = min < c ? min : c;
return a + b + c - max - min;
}
2.计算两个数的和
int sum(int x, int y)
{
return x + y;
}
typedef int (^ MyBlock) (int, int);
//原型: int (^)(int, int)
//别名: MyBlock
//定义一个全局变量num
int num = 10;
int main(int argc, const char * argv[]) {
@autoreleasepool {
1.取中间值
printf("%d", middle(10, 20, 12));
输出的中间值为12
2.计算两个数的和
int (*p)(int, int) = sum;
printf("\n%d", p(10, 12));
输出的和为22
/*
Block: 块语法 (本质是匿名函数)
^: 指向符 用来声明Block类型变量
=左面: 返回值类型 (^ 变量名) (参数1类型, 参数2类型...)
=右面: ^ [返回值类型] (参数1类型 参数名, 参数2类型 参数名)
{
实现部分
}
注意: 等号左右两侧参数类型一定要一一对应, 等号右侧参数类型后面, 一定要指定参数名
*/
1.定义一个无参数, 无返回值的Block
void (^ firstBlock)(void) = ^ void (void){
NSLog(@"这是第一个Block");
};
firstBlock();
//2.定义一个有参数,无返回值的Block
void (^ secondBlock)(int, int) = ^ (int a, int b){
NSLog(@"%d", a + b);
};
secondBlock(12, 12);
//3.定义一个无参数, 有返回值的Block, 返回班级人数
int (^ thirdBlock)() = ^(){
return 40;
};
NSLog(@"%d", thirdBlock());
//4.定义一个有参数, 有返回值的Block, 计算最小公倍数
int (^ forthBlock)(int, int) = ^ (int a, int b){
int min = a < b ? a : b;
int GYS = 0;
for (int i = min; i >= 1; i--) {
if (a % i == 0 && b % i == 0) {
GYS = i;
break;
}
}
return a * b / GYS;
};
NSLog(@"%d", forthBlock(6, 36));
//5.写一个 返回值为整型 参数为NSString(仅一个参数)的block,实现将字符串转换为整型的功能。
NSInteger (^ fifthBlock)(NSString *) = ^ (NSString *str){
return [str integerValue];
};
NSLog(@"%ld", fifthBlock(@"123456"));
MyBlock myBlock = ^ (int a, int b){
return a > b ? a : b;
};
NSLog(@"%d", myBlock(12, 10));
MyBlock yourBlock = ^ (int x, int y){
return x + y;
};
NSLog(@"%d", yourBlock(5, 6));
//Block与局部变量
//Block默认在全局区, 如果Block内部使用了局部变量, 那么将会Block拷贝到栈区.
//Block内部只能访问局部变量, 不能更改; 如果想更改局部变量, 需要在局部变量声明之前加上__block, 将局部变量拷贝到block所在的栈区中.
// __block int num = 10;
//Block与全局变量的关系, Block既可以访问全局变量, 又可以更改全局变量
void (^ printNumBlock)() = ^ (){
NSLog(@"%d", num++);
};
printNumBlock();
NSLog(@"%@", printNumBlock);
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"02", @"23", @"12", @"10", @"08", nil];
//比较器: 返回值, 返回当前两个对象比较的状态 (升序, 降序, 相等)
NSComparator compareCustormer = ^(NSString *str1, NSString *str2){
NSLog(@"str1 = %@, str2 = %@", str1, str2);
return [str1 compare:str2];
};
//将比较器的返回结果进行判断, 如果是降序状态, 进行交换.
//默认升序排列, 如果想降序, 那么需要将比较器中的返回值取反.
NSArray *result = [array sortedArrayUsingComparator:compareCustormer];
NSLog(@"%@", result);
字面量是iOS中一种简化创建字符串, 数组, 字典的方式.
#warning 注意: 而且一定是不可变的
例如:
NSString *str = @"字符串";
//等价于
NSString *str2 = [NSString stringWithUTF8String:"字符串"];
例如:
NSArray *array = @[student1, student2, student3];
//等价于
NSArray *array2 = [NSArray arrayWithObjects:student1, student2, student3, nil];
//访问数组内的元素array[0] 等价于 [array objectAtIndex:0]
例如:
NSDictionary *dictionary = @{@"1":student1, @"2":student2, @"3":student3};
//等价于
NSDictionary *dictionaryTwo = [NSDictionary dictionaryWithObjects:@[student1, student2, student3] forKeys:@[@"1", @"2", @"3"]];
//访问字典内的元素dictionary[key]等价于[dictionary objectForKey:key]
例如:
NSLog(@"%@", [dictionary objectForKey:@"1"]);
NSLog(@"%@", dictionary [@"1"]);