block系列重新整理3:循环引用

来源:互联网 发布:iphone摄影软件推荐 编辑:程序博客网 时间:2024/04/29 12:12

AppDelegate.m

#import "AppDelegate.h"#import "Person.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];        //循环引用 第一种方式//    __block Person *ps = [[Person alloc] init];//    ps.myblock = ^{//        [ps test];//    };//    [ps loadData];//    [ps release];            //循环引用 第二种方式    Person *ps = [[Person alloc] init];    [ps printTest];    [ps release];        return YES;}

Person.h

typedef void(^Personblock)();@interface Person : NSObject{    NSString *name;}@property (nonatomic, copy)Personblock myblock;- (void)test;- (void)loadData;- (void)printTest;

Person.m

- (void)test{    NSLog(@"print test");}- (void)loadData{    _myblock();}- (void)printTest{    __block Person *ps = self;    self.myblock = ^{        ps->name = @"jack";        [ps test];    };        self.myblock();}- (void)dealloc{    NSLog(@"%s",__FUNCTION__);        //二选一//    [_myblock release];    Block_release(_myblock);    [super dealloc];}


0 0
原创粉丝点击