Block语法 块语法,本质上是匿名函数(没有名称的函数)C++、JS、Swift等语言,有类似语法,叫做闭包
//Block 的作用,主要是用于回调函数,解耦合
//当方法的参数是block类型,block的参数用户从内向外传值;block的返回值,用于,从外向内返回结果
#import <Foundation/Foundation.h>
#import "XunleiDownload.h"
#import "LoginManager.h"
#import "Person.h"
//int maXValue(int a, int b);
//int maXValue(int a, int b)
//{
// return a > b ? a : b;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
//Block语法 块语法,本质上是匿名函数(没有名称的函数)C++、JS、Swift等语言,有类似语法,叫做闭包
// int (*p)(int, int) = maXValue;
// int result = p(10, 14);
// NSLog(@"result = %d",result);
//block本质是一个匿名函数,可以通过block把一个函数定义成一个变量
// BOLCK和函数的相似性:(1)可以保存代码(2)有返回值(3)有形参(4)调用方式一样。
//int (^)(int,int) block变量类型
//myblock 变量名
//变量的值(值是一个匿名函数)
//int (int a, int b)
//{
// return a > b ? a : b;
//};//[有分号;]
// int (^myBlock)(int, int) = ^int (int a, int b)
// {
// return a > b ? a : b;
// };
// int result = myBlock(15, 10);
// NSLog(@"%d",result);
// int (^myblock)(int) = ^int (int a)
// {
// int b = a * a;
// return b;
// };
// int result = myblock(5);
// NSLog(@"%d",result);
//
//无参无返回值
//void (^helloBlock)(void) = ^void (void)
//给block赋值的匿名函数,返回值类型可以直接省略不写,如果没有参数,参数列表也可以省略
// void (^helloBlock)() = ^
// {
// NSLog(@"hello, world!");
// };
// helloBlock();
// NSArray (^array)(int a) = ^(int a)
// int (^stringForInt)(NSString *) = ^int (NSString * a){
// int b = [a intValue];
// return b;
// };
// int result = stringForInt(@"59");
// NSLog(@"%d", result);
// NSString * (^myBlock) (NSString *, NSString *) = ^NSString * (NSString * a, NSString * b){
// NSArray * str = [a componentsSeparatedByString:b];
// NSString * str1 = [str firstObject];
// return str1;
// };
// NSString * getting = myBlock(@"www.baidu.com",@".c");
// NSLog(@"%@",getting);
//NSArray * array = [NSArray arrayWithObjects:@"henan", @"lanou", @"ios", @"17ban", nil];
// NSString * (^myBlock)(NSArray *) = ^NSString * (NSArray * b){
// NSString * str = @"";
// for (int i = 0; i < [b count]; i++) {
// str = [str stringByAppendingString:[b objectAtIndex:i]];
// }
// return str;
// };
// //NSArray * array = [NSArray arrayWithObjects:@"河南", @"蓝欧", @"ios", @"17班", nil];
// NSString * string = myBlock([NSArray arrayWithObjects:@"河南", @"蓝欧", @"ios", @"17班", nil]);
// NSLog(@"%@",string);
// NSString * (^appendString)(NSArray *) = ^(NSArray * array){
// NSMutableString * string = [NSMutableString stringWithCapacity:1];
// for (NSString * str in array) {
// [string appendString:str];
// }
// return string;
// };
// NSString * string1 = appendString([NSArray arrayWithObjects:@"河南", @"蓝欧", @"ios", @"17班", nil]);
// NSLog(@"%@",string1);
// NSString * (^appendString)(NSArray *) = ^(NSArray * array){
// NSString * string = @"";
// for (NSString * str in array) {
// string = [string stringByAppendingString:str];
// }
// return string;
// };
//
// NSString * string1 = appendString([NSArray arrayWithObjects:@"河南", @"蓝欧", @"ios", @"17班", nil]);
// NSLog(@"%@",string1);
// NSString * (^appendString)(NSArray *) = ^(NSArray * array){
// NSString * string = @"";
// for (NSString * str in array) {
// string = [NSString stringWithFormat:@"%@%@", string, str];
// }
// return string;
// };
//
// NSString * string1 = appendString([NSArray arrayWithObjects:@"河南", @"蓝欧", @"ios", @"17班", nil]);
// NSLog(@"%@",string1);
// XunleiDownload * dow = [[XunleiDownload alloc] init];
//// [dow download:^(int n) {
//// NSLog(@"绘制 %d 个矩形", n);
//// }];
// [dow download:^(int n) {
// NSLog(@"添加 %d 个圆", n);
// }];
// LoginManager * manager = [[LoginManager alloc] init];
// [manager loginWithName:@"zhangsan" password:@"123456" successful:^(NSString *userName) {
// // NSLog(@"显示用户名信息,用户名是%@",userName);
//
// NSLog(@"%@ 发小纸条",userName);
// }];
// LoginManager * manager = [[LoginManager alloc] init];
// [manager loginWithName:@"zhangsan" password:@"123456" successful:^(NSString *userName) {
// NSLog(@"%@ 发小字条",userName);
// } failure:^(NSString *userName) {
// NSLog(@"用户名 %@ 不存在",userName);
// }];
// LoginManager * manager = [[LoginManager alloc] init];
// [manager loginWithName:@"zhangsan" password:@"12345" successful:^int(NSString *userName) {
// NSLog(@"%@ 发小字条",userName);
// } failure:^BOOL(NSString *errorMassage) {
// NSLog(@"%@",errorMassage);
// return NO;
// }];
// [manager loginWithName:@"zhangsan" password:@"123456" successful:^void(NSString *userName) {
// NSLog(@"%@ 发小字条",userName);
// } failure:^BOOL(NSString *errorMassage) {
// NSLog(@"%@",errorMassage);
// return YES;
// }];
// NSArray * array = [NSArray arrayWithObjects:@"zhangsan", @"lisi", @"wanwu", @"cuiyayun", @"yingsasa", nil];
// NSArray * newArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
// return [obj1 compare:obj2];
// }];
// NSLog(@"%@",newArray);
/*
Person * p1 = [[Person alloc] initWithName:@"zhansan" sex:@"M" age:@"19"];
Person * p2 = [[Person alloc] initWithName:@"lisi" sex:@"M" age:@"26"];
Person * p3 = [[Person alloc] initWithName:@"yangliu" sex:@"W" age:@"22"];
Person * p4 = [[Person alloc] initWithName:@"heinei" sex:@"W" age:@"20"];
Person * p5 = [[Person alloc] initWithName:@"wangwu" sex:@"M" age:@"17"];
NSArray * array = [[NSArray alloc] initWithObjects:p1, p2, p3, p4, p5, nil];
NSArray * newArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
Person * per1 = (Person *)obj1;//强转类型
Person * per2 = (Person *)obj2;
if (per1.age < per2.age) {
return NSOrderedAscending;
}else if (per2.age < per1.age){
return NSOrderedDescending;
}
return NSOrderedSame;
}];
NSLog(@"%@",newArray);
*/
__block int m = 3;
//在block中使用block以外的变量,是只读的,要想该变量能在block中被修饰,使用__block进行修饰
void (^printNumberBlock)(int a) = ^(int a){
m = m * a;
NSLog(@"%d",m);
};
printNumberBlock(5);
}
return 0;
}