您的位置:首页 > 其它

Block生命周期

2016-02-24 13:09 337 查看
#import <UIKit/UIKit.h>

typedef NSString*(^MyBlock) (NSString*,NSInteger);

@interface ViewController : UIViewController

-(MyBlock)GetDefaltBlock:(NSString*)Astr withInt:(NSInteger) Aint;
@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
MyBlock _b = [self GetDefaltBlock:@"Hello" withInt:123];
NSLog(@"%@",_b(@"World",456));
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

-(MyBlock)GetDefaltBlock:(NSString *)Astr withInt:(NSInteger)Aint
{
MyBlock b = ^NSString*(NSString* Bstr, NSInteger Bint)
{
return [NSString stringWithFormat:@"STR IS %@,INT IS %ld",
[Astr stringByAppendingString:Bstr],Aint + Bint];
};
return b;
}

@end

这个居然也能行,不可思议啊,一直以为访问不到astr运行的时候会有错误。

arc环境下,还挺神奇。

非arc环境参考http://bbs.9ria.com/thread-259515-1-1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: