3Block
3.1是一种新的数据(类型)
3.2Block数据类型的Block(变量)存储的是一段儿代码
int a ;
void(^myBlock)(void)=^void(void)//定义了一个Block的变量
{
[self print:@"Block被执行了"];
};
myBlock();
double(^myBlock1)(double a,double b);
myBlock1 = ^double(double a,double b)
{
return a + b;
};
[self print:[NSNumber numberWithDouble: myBlock1(3,5)]];
3.3语法
3.3.1没有固定的关键字
3.3.2其数据类型是由该变量存储代码的返回值类型+形参构成
3.3.3可以使用typedef指定Block数据类型名如:
#import "ViewController+TRPrint.h"
typedef void(^BlockType)(void);
typedef double(^BlockType1)(double a, double b);
@interface ViewController ()
BlockType b1 = ^void(void)//也可写成BlockType b1 = ^,因为返回值类型是void
{
[self print:@"用Block类型定义变量"];
};
b1();
BlockType1 b2 = ^double(double a, double b)
{
return a - b;
};
[self print:[NSNumber numberWithDouble:b2(3,5)]];
3.4Block与全局变量
在扩展里写个定义个全局变量
int g_i = 10;//一个全局变量
然后在viewDidLoad里测试
//Block与全局变量
BlockType b3 = ^
{
[self print:[NSNumber numberWithInt:g_i]];//全局变量在Block中可以被访问
g_i = 20;//全局变量在Block中可以被赋值
};
b3();
[self print:[NSNumber numberWithInt:g_i]];
3.5Block与局部变量
//Block与局部变量
int i2 =30;
__block int i3 = 50;
BlockType b4 = ^
{
[self print:[NSNumber numberWithInt:i2]];//局部变量在Block中可以被访问
//i2 = 40;//局部变量在Block中不可以被赋值
i3 = 60;//加上关键字__block后,局部变量才可以在Block中赋值
};
b4();
[self print:[NSNumber numberWithInt:i3]];
3.6Block在自定义类中的使用
TRMyclass.h
typedef void(^MyBlock)(void);
typedef double (^MyBlock1)(double a,double b);
typedef NSString*(^MyBlock2)(void);
@interface TRMyclass : NSObject
-(void)mehtod:(MyBlock)b;
-(NSString*)method1:(MyBlock1)b;
-(MyBlock2)getBlock;
@property MyBlock b;
@end
TRMyclass.m
@implementation TRMyclass
-(void)mehtod:(MyBlock)b
{
b();//callback回调
}
-(NSString *)method1:(MyBlock1)b
{
return [NSString stringWithFormat:@"%g",b(2,3)];
}
-(MyBlock2)getBlock
{
return ^NSString*(void){return @"返回一个block";};
}
在ViewController.m
//Block在自定义类中的使用
TRMyclass *m = [[TRMyclass alloc]init];
[m mehtod:^{
[self print:@"Block做方法形参"];
}];
[m mehtod:^{
[self print:@"这是多态的表现"];
}];
[self print:[m method1:^double(double a, double b) {
return a + b;
}]];
[self print:[m method1:^double(double a, double b) {
return a - b;
}]];
[self print:[m getBlock]()];//Block本质上函数指针
m.b = ^
{
[self print:@"Block做属性"];
};
m.b();
}
3.7Block与数组、字典
//Block与数组、字典
TRStudent *stu1 = [TRStudent studentWithName:@"张三" andAge:18];
TRStudent *stu2 = [TRStudent studentWithName:@"李四" andAge:20];
TRStudent *stu3 = [TRStudent studentWithName:@"王五" andAge:19];
NSComparisonResult(^sort)(id _Nonnull obj1, id _Nonnull obj2)= ^NSComparisonResult(id obj1, id obj2) {
NSNumber *selfAge = [NSNumber numberWithInt:[obj1 age]];
NSNumber *otherAge = [NSNumber numberWithInt:[obj2 age]];
return [selfAge compare:otherAge];
};
//Block与数组
NSArray *stuArray = @[stu1,stu2,stu3];
NSArray *sortedArray = [stuArray sortedArrayUsingComparator:sort];
NSMutableString *str = [NSMutableString stringWithCapacity:15 *3];
for(TRStudent *s in sortedArray)
{
[str appendFormat:@"%@\n",s];
}
self.outputLabel.text = str;
//Block与字典
NSDictionary *stuDict = @{@"1":stu1,@"2":stu2,@"3":stu3};
NSArray *sortedDict = [stuDict keysSortedByValueUsingComparator:sort];
str = [NSMutableString stringWithCapacity:15 *3];
for (NSString *key in sortedDict)
{
[str appendFormat:@"%@",stuDict[key]];
}
[self print:str];
}
网友评论