這些iOS面試官一點(diǎn)不靠譜,到底自己做過iOS沒啊

場(chǎng)景1:
這是一道筆試題

image.png

我說打印 10 10 100 100
面試官非說我可能不太理解block,最后一個(gè)不應(yīng)該打印100


場(chǎng)景2:
一個(gè)vc上有 a b c 三個(gè)view,如果vc和a 相互強(qiáng)引用,那么vc在pop后,b c會(huì)不會(huì)走dealloc方法

我的理解是a b c vc都不會(huì)走dealloc方法,但面試官給我的說法是他們打印過,說 b c會(huì)走dealloc方法。
我說不走的理解是:
如果vc都不釋放,self.view也不會(huì)釋放,那么自然也不會(huì)移除self.view上的子控件abc,所以這種場(chǎng)景下vc a b c 都不會(huì)走dealloc方法。
寫代碼驗(yàn)證,我的理解是正確的。


@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強(qiáng)引用 vc,就會(huì)導(dǎo)致 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


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容