天天看點

UI控件用weak還是用strong修飾

  1. #import "ViewController.h"  
  2. @interface ViewController ()  
  3. @property (nonatomic, weak) UIView *weakView;  
  4. @property (nonatomic, weak) UIView *selfWeakView;  
  5. @property (nonatomic, strong) UIView *strongView;  
  6. @property (nonatomic, strong) UIView *selfStrongView;  
  7. @end  
  8. @implementation ViewController  
  9. - (void)viewDidLoad  
  10. {  
  11.     [super viewDidLoad];  
  12.     self.selfStrongView = [[UIView alloc]initWithFrame:CGRectMake(25, 25, 50, 50)];  
  13.     printf("self.selfStrongViewretain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.selfStrongView)));  
  14.     [self.view addSubview:self.selfStrongView];  
  15.     printf("self.selfStrongView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.selfStrongView)));  
  16.     _strongView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];  
  17.     printf("_strongView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_strongView)));  
  18.     [self.view addSubview:self.strongView];  
  19.     printf("_strongView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_strongView)));  
  20.     UIView *selfView = [[UIView alloc]initWithFrame:CGRectMake(150, 150, 50, 50)];  
  21.     self.selfWeakView = selfView;  
  22.     printf("self.selfWeakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.selfWeakView)));  
  23.     [self.view addSubview:self.selfWeakView];  
  24.     printf("self.selfWeakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.selfWeakView)));  
  25.     UIView *weaksView = [[UIView alloc]initWithFrame:CGRectMake(200, 200, 50, 50)];  
  26.     printf("weakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(weaksView)));  
  27.     _weakView = weaksView;  
  28.     printf("_weakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_weakView)));  
  29.     [self.view addSubview:_weakView];  
  30.     printf("_weakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_weakView)));  
  31. }  
  32. - (void)viewWillAppear:(BOOL)animated  
  33. {  
  34.     printf("self.selfStrongView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_strongView)));  
  35.     printf("_strongView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_strongView)));  
  36.     printf("self.selfWeakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(self.selfWeakView)));  
  37.     printf("_weakView retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(_weakView)));  
  38. }  
  39. @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。