第一種情況直接weak修飾后初始化劲适,編譯器會(huì)提示警告:Assigning retained object to weak property; object will be released after assignment,賦值后會(huì)被釋放
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, weak) UIView *bview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.bview = [[UIView alloc] init];
NSLog(@"%@",self.bview);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"點(diǎn)擊屏幕后%@",self.bview);
}
@end
結(jié)果
2023-11-29 10:32:51.003871+0800 OCDemo[82234:2684446] (null)
2023-11-29 10:33:01.251990+0800 OCDemo[82234:2684446] 點(diǎn)擊屏幕后(null)
第二種情況用weak修飾賦值
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, weak) UIView *bview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIView *tempView = [[UIView alloc] init];
self.bview = tempView;
NSLog(@"%@",self.bview);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"點(diǎn)擊屏幕后%@",self.bview);
}
2023-11-29 10:36:17.927977+0800 OCDemo[82441:2688402] <UIView: 0x7f81ac704240; frame = (0 0; 0 0); layer = <CALayer: 0x600000a78340>>
2023-11-29 10:36:30.112701+0800 OCDemo[82441:2688402] 點(diǎn)擊屏幕后(null)
第三種情況用weak修飾賦值后添加到view上
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, weak) UIView *bview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIView *tempView = [[UIView alloc] init];
self.bview = tempView;
[self.view addSubview:self.bview];
NSLog(@"%@",self.bview);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"點(diǎn)擊屏幕后%@",self.bview);
}
2023-11-29 10:38:46.851065+0800 OCDemo[82679:2691860] <UIView: 0x7faec2f0c010; frame = (0 0; 0 0); layer = <CALayer: 0x6000000ab800>>
2023-11-29 10:38:48.487691+0800 OCDemo[82679:2691860] 點(diǎn)擊屏幕后<UIView: 0x7faec2f0c010; frame = (0 0; 0 0); layer = <CALayer: 0x6000000ab800>>
最后一種情況為啥不被釋放呢,原因是當(dāng)我們執(zhí)行了addSubview:方法之后,后面的視圖就會(huì)被放到這個(gè)subViews數(shù)組里,可以看到,這個(gè)數(shù)組使用copy修飾的,也就是說孙技,這是強(qiáng)引用!正是這個(gè)原因排作,我們才能用weak來修飾一個(gè)控件牵啦。因此可以保持這個(gè)控件的引用計(jì)數(shù)不為0,就不會(huì)被釋放掉了妄痪。
@property(nonatomic,readonly,copy) NSArray<__kindof UIView *> *subviews;
總結(jié)一下:一般情況用weak哈雏,strong都可以的,但是特殊的種情況如果創(chuàng)建一個(gè)view用weak修飾拌夏,沒有add到其他view上僧著,那這個(gè)view就沒有和別的view有強(qiáng)引用關(guān)系,引用計(jì)數(shù)也沒有+1障簿,所以會(huì)被釋放盹愚,反之換成strong修飾的話,它本身就引用計(jì)數(shù)+1了站故,不管有沒有被add到其他view上都不會(huì)被釋放皆怕,所以不想這么糾結(jié)就都用strong修飾吧
還有個(gè)問題解釋一下,strong修飾本身引用計(jì)數(shù)+1西篓,addSubView后又+1愈腾,雖然引用計(jì)數(shù)為2,但是當(dāng)整個(gè)父視圖被銷毀的時(shí)候岂津,這兩個(gè)引用計(jì)數(shù)都會(huì)變成0虱黄,所以不會(huì)造成內(nèi)存泄漏。
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者