自编一道block面试题

日常开发维护新老代码经常会遇到cell 在代理方法中回调block的问题. 这些坑大多数开发都踩到过.
由此自己编了一道面试题,自检一下. (就不公布结果了)

题目问题是,下面代码打印结果是多少?

#import "ViewController.h"

@interface QYUser : NSObject

@property(nonatomic, strong) NSString *name;

@end

@implementation QYUser

@end

@interface ViewController ()

@property(nonatomic, strong) QYUser *user;
@property(nonatomic, strong) void (^testBlock)(void);
@property(nonatomic, assign) BOOL isContainFlag;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.user = [QYUser new];
    
    [self test1];
    
    NSLog(@"分割线================");
    
    [self test2];
}

- (void)test1 {
    self.user.name = @"AAA";
    [self logUserName];
    self.testBlock();
   
    self.user.name = @"BBB";
    [self logUserName];
    self.testBlock();
    
    self.user.name = @"CCC";
    [self logUserName];
    self.testBlock();
}

- (void)test2 {
    self.user = [[QYUser alloc] init];
    self.user.name = @"AAA";
    [self logUserName];
    self.testBlock();
   
    self.user = [[QYUser alloc] init];
    self.user.name = @"BBB";
    [self logUserName];
    self.testBlock();
    
    self.user = [[QYUser alloc] init];
    self.user.name = @"CCC";
    [self logUserName];
    self.testBlock();
}

- (void)logUserName {
    QYUser *testUser = self.user;
    
    if (!self.isContainFlag) {
        self.isContainFlag = YES;
        
        self.testBlock = ^{
            NSLog(@"====%@",testUser.name);
        };
    }
}

@end

推荐阅读更多精彩内容