这些iOS面试官一点不靠谱,到底自己做过iOS没啊

场景1:
这是一道笔试题

image.png

我说打印 10 10 100 100
面试官非说我可能不太理解block,最后一个不应该打印100


场景2:
一个vc上有 a b c 三个view,如果vc和a 相互强引用,那么vc在pop后,b c会不会走dealloc方法

我的理解是a b c vc都不会走dealloc方法,但面试官给我的说法是他们打印过,说 b c会走dealloc方法。
我说不走的理解是:
如果vc都不释放,self.view也不会释放,那么自然也不会移除self.view上的子控件abc,所以这种场景下vc a b c 都不会走dealloc方法。
写代码验证,我的理解是正确的。


@import UIKit;

@interface ViewControllerA : UIViewController
@end
@interface ViewControllerB : UIViewController
@end
@interface A : UIView

@end
@interface B : UIView

@end
@interface C : UIView
@property(nonatomic) UIViewController *vc;
@end
@implementation ViewControllerA

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.navigationController pushViewController:[ViewControllerB new] animated:YES];
}

@end



@implementation ViewControllerB
- (void)dealloc
{
    NSLog(@"---%@---",@"ff");
}
+ (instancetype)new {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    return [sb instantiateViewControllerWithIdentifier:@"ViewControllerB"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview: [A new]];
    [self.view addSubview: [B new]];
    C *aC = [C new];
    /// 这里view强引用 vc,就会导致 vc 不释放
    aC.vc = self;
    [self.view addSubview: aC];

}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
@implementation A

- (void)dealloc
{
    NSLog(@"---%@---",@"A---");
}

@end
@implementation B

- (void)dealloc
{
    NSLog(@"---%@---",@"B---");
}

@end
@implementation C

- (void)dealloc
{
    NSLog(@"---%@---",@"C---");
}

@end


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容