UI控制項用weak還是用strong修飾

來源:互聯網
上載者:User
#import "ViewController.h"@interface ViewController ()@property (nonatomic, weak) UIView *weakView;@property (nonatomic, weak) UIView *selfWeakView;@property (nonatomic, strong) UIView *strongView;@property (nonatomic, strong) UIView *selfStrongView;@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    self.selfStrongView = [[UIView alloc]initWithFrame:CGRectMake(25, 25, 50, 50)];    printf("self.selfStrongViewretain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.selfStrongView)));    [self.view addSubview:self.selfStrongView];    printf("self.selfStrongView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.selfStrongView)));        _strongView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];    printf("_strongView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_strongView)));    [self.view addSubview:self.strongView];    printf("_strongView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_strongView)));    UIView *selfView = [[UIView alloc]initWithFrame:CGRectMake(150, 150, 50, 50)];    self.selfWeakView = selfView;    printf("self.selfWeakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.selfWeakView)));    [self.view addSubview:self.selfWeakView];    printf("self.selfWeakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.selfWeakView)));        UIView *weaksView = [[UIView alloc]initWithFrame:CGRectMake(200, 200, 50, 50)];    printf("weakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(weaksView)));    _weakView = weaksView;    printf("_weakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_weakView)));    [self.view addSubview:_weakView];    printf("_weakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_weakView)));}- (void)viewWillAppear:(BOOL)animated{    printf("self.selfStrongView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_strongView)));    printf("_strongView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_strongView)));    printf("self.selfWeakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.selfWeakView)));    printf("_weakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_weakView)));}@end


在ARC中,對象釋放的最終根據還是根據引用計數為0時去釋放。而weak與strong的根本區別是在set方法中,weak的set方法和strong的set方法都是釋放舊值保留新值,但是weak的set方法會對其autorelease,即延遲release一次,而strong的set方法也是釋放舊值保留新值,但是其不會延遲release。最終效果是strong會+1,weak不會+1.
要注意,用_去賦值的時候是不調用set方法的,也就是說無論weak還是strong,只要用_賦值都不會對引用計數加1,區別在於self.文法會調用set方法,strong的self.會調用set方法+1.而weak的_和self.文法都不會+1.
所以,建議用weak,用weak時無論用_和.文法都不會導致+1。用strong時用self.文法會導致+1.建議用weak。同時weak在對象回收以後可以將對象指標置成nil。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.