您的位置:首页 > 其它

关于Block块语法的那些事!

2015-12-12 19:57 489 查看
#import <Foundation/Foundation.h>
#import "Sum.h"
#import "Student.h"

int global = 100 ;

int main(int argc, const char * argv[]) {
@autoreleasepool {

NSLog(@"%i",sum(3,4));
int (*p) (int a ,int b ) = sum; // 调用函数指针
NSLog(@"%i",p(3,4));
//
//        Block 块语法,本质上是匿名函数
//        block 的语法格式
/*
返回值类型(^block名)(参数列表) = ^(参数列表){
};
= 左边是block的声明,右边是block的实现
*/

//无参无返block
void (^myblock)(void) = ^(void){
NSLog(@"HAHSF");
};
myblock();
// 有参无返
void (^block1)(int ,int ) = ^( int a ,int b ){
NSLog(@"%d",a+b);
};
block1(5,6);
//有返无参
int (^block3)(void) = ^(void){
return 8;
};
int i = block3();
NSLog(@"%i",i);
// 有参有返
int (^block2) (int ,int ) = ^(int a ,int b){
return a+b;
};
int c = block2(2 , 3);
NSLog(@"%i",c);

// block 的typedef

typedef int (^ Block) (int ,int );
Block block4 = ^(int a ,int b){
return a*b;
};
int mul = block4(5,6);
NSLog(@"%i",mul);

// 练习
{
//        写一个 返回值为整型 参数为NSString(仅一个参
//        数)的block,实现将字符串转换为整型的功能。
int (^block)(NSString *) = ^(NSString *str){
return [str intValue];
};
int a = block(@"46851thsdf");
NSLog(@"%d",a);
}
//     block 跟全局变量
void(^block11)(void) = ^(void){
global++;
};
block11();
NSLog(@"%d",global);

__block int a = 10 ;
// 当代吗执行到block的定义时,并不会执行block的实现代码,但是会将block内部使用的局部变量拷贝一份,存入另一块内存空间,此时局部变量和Block内部使用的局部变量已经不是同一个,可以在定义局部变量的时候,加__block修饰,避免此问题
int (^block12)(int ) = ^(int b){
NSLog(@"%p",&a);
a = 10 + a;
return a+b;
};
int l = block12(5);
NSLog(@"%d",l);
a= 30;
int k = block12(5);
NSLog(@"%d",k);

// block跟数组排序
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"s",@"g",@"c",@"a", nil];
[arr sortUsingSelector:@selector(compare:)];

[arr sortUsingComparator:^
NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
return -1 * [obj1 compare:obj2];
}];
NSLog(@"%@",arr);

NSComparisonResult (^sortBlock) (NSString*,NSString*) = ^(NSString *str1,NSString *str2){
return [str1 compare:str2];
};
[arr sortUsingComparator:sortBlock];
NSLog(@"%@",arr);
// 练习
{
Student *stu1 = [Student studentWithName:@"lnhif" age:15 number:@"8"];
Student *stu2 = [Student studentWithName:@"gr" age:20 number:@"4"];
Student *stu3 = [Student studentWithName:@"rgh" age:16 number:@"7"];
NSMutableArray *mArr = [[NSMutableArray alloc]initWithObjects:stu1,stu2,stu3, nil];
typedef NSComparisonResult (^Block)(Student * ,Student *);
Block blockArr = ^(Student *st1,Student *st2){
if (st1._age > st2._age) {
return NSOrderedAscending;
}
else if(st1._age == st2._age)
{
return NSOrderedSame;
}else
{
return  NSOrderedDescending;
}
};
[mArr sortUsingComparator:blockArr];
NSLog(@"%@",mArr);
NSComparisonResult (^blockArr1)(Student * ,Student *) = ^(Student *p,Student *q){
return -1 *[p._name compare:q._name];
};
[mArr sortUsingComparator:blockArr1];
NSLog(@"%@",mArr);

NSComparisonResult (^blockArr2)(Student *,Student *) = ^(Student *p,Student *q){
return -1*[p._number compare:q._number];      };
[mArr sortUsingComparator:blockArr2];
NSLog(@"%@",mArr);
}
//字面量
//字符串
NSString *str = @"asjoefhuwbf";
//shuzu
NSArray *NSarr = @[@"sgd",@"564561"];
//字典
NSDictionary *dic = @{@"key1":@"value1",@"key2":@"value2"};
// NSnumber
NSNumber *num = @(3);

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: