Block的使用

2016-08-24  本文已影响0人  我的肖

#import

@interfaceViewController :UIViewController

//block 定义 无返回值 传值obj

typedefvoid(^myblock)(idobj);

-(void)creatandblock:(myblock)block;

@end

@interfaceViewController()

@property(nonatomic,assign)intnumber;

//定义一个返回值为Void,名为blockBack的Block。传值为字符串。

@property(nonatomic,copy)void(^blockBack)(NSString*valuename);

@end

//定义block

typedefvoid(^myblock)(idobj);

@implementationViewController

//添加Block的类型的进行传值

-(void)creatandblock:(myblock)block

{

}

- (void)viewDidLoad {

[superviewDidLoad];

[selfcreatNSstring:@"xiao"andBlock:^(idobj) {

obj =@"abc";

}];

}

-(void)creatwithBlock:(myblock)block

{

}

-(void)creatNSstring:(NSString*)name andBlock:(myblock)block

{

block(@"acb");

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

}

@end

在iOS 编程中发现Capturng 'self' strongly in this block is likely to lead to a retain cycle 的警告,告诉其,造成的是Block代码块的循环引用。

使用方法

__weaktypeof(self) weakself =self;

self.popView.selectRowAnIndex= ^(NSIntegerindex)

{

NSString*str =  weakself.array[index];

NSLog(@"%@",str);

};

就是将self用弱引用的weakself替代。

主要造成循环引用的问题是 当前类调用Block ,block内部还有当前类,就会造成循环引用,可以将当前的类用__weak typeof(当前类) weak+当前类 = 当前类

void (^dealingResult)(NSArray *)= ^(NSArray *statues){

设计的一个block调用直接dealingResult();返回值为void

};

上一篇 下一篇

猜你喜欢

热点阅读