ios开发--block应该知道的那几件事

block 现在已经基本上已经使用非常普遍了,在使用用中也又很多坑,下面把block开发中应该知道的那几件事总结下.

内联block 说白了就是block被嵌入到一个函数中
例如:

typedef void (^HelloBlock)(void);

- (void) BlockTest{
HelloBlock myBlock= ^(void){
NSLog(@"Hello Block");
};
myBlock();
}

下面的写法如果不加上_block a无法dealloc

__block A  a =[ [[A alloc] init] withBlock:^{

 [a action];

 [ a release];

}]; //这样A的dealloc方法就会调用
@interface ViewControllerUIViewViewController 
 {
        NSString *_string;
 }

 __block ViewController *controller = self;   
    _block = ^{
         NSLog(@"string %@",controller->_string);
    };
#import <UIKit/UIKit.h>   
@interface AppDelegate : NSObject <UIApplicationDelegate>
@property (nonatomic, strong) NSString *stringProperty;  
@end
#import "GCDAppDelegate.h" 
@implementation AppDelegate 
@synthesize stringProperty;  
- (void) BlockTestError{
HelloBlock myBlock= ^(void){
	self.stringProperty = @"Block Objects"; 
	NSLog(@"String property = %@", self.stringProperty);//运行错误
};
myBlock();
}}  

- (void) BlockTestCorrect{
	HelloBlock myBlock= ^(void){
		[self setStringProperty:@"Block Objects"];
	    NSLog(@"self.stringProperty = %@", [self stringProperty]); //运行ok
	};
myBlock();
}}  
@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值