zoukankan      html  css  js  c++  java
  • strong & weak 的理解

      1 import "ViewController.h"
      2 
      3 @interface ViewController ()
      4 /*weak*/
      5 @property (nonatomic,weak)UISwitch *weakSwitch;
      6 /*strong*/
      7 @property (nonatomic ,strong)UISwitch *strongSwitch;
      8 /*weak*/
      9 @property (nonatomic,weak)UIButton *weakButton;
     10 @end
     11 
     12 @implementation ViewController
     13 
     14 /*
     15  1:strong:代表强引用,
     16  2:weak:代表弱引用,
     17  3:我们创建的变量,默认都是强引用
     18  */
     19 
     20 - (void)viewDidLoad {
     21     [super viewDidLoad];
     22     
     23     /*
     24      1:局部变量:在该变量创建完对象,就默认有一个强引用,但是当局部变量所在的方法执行完毕后,局部变量就会被销毁。
     25      2:变量用__weak 和 __strong修饰,创建变量时,默认都为__strong,用__weak修饰,如:
     26       __weak  UIButton *btn = [[UIButton alloc]init];
     27      则变量该创建完毕就会被销毁,可以通过打印变量的内存地址:%p打印内存地址来查看有没有被销毁
     28      
     29      */
     30     
     31     //1:__weak和__strong的理解
     32     __weak  UIButton *btn = [[UIButton alloc]init];
     33      NSLog(@"------1----btn=%p-------",btn);//btn对象被销毁
     34     UILabel *label = [[UILabel alloc]init];//label为局部变量,对象没被立即销毁,直到viewDidLoad方法执行完毕后销毁
     35     
     36     //2:属性weak,strong的理解:
     37     
     38      //weak引用:也就是弱指针引用:创建完对象后立即被销毁
     39      self.weakSwitch = [[UISwitch alloc]init];//创建完对象后立即被销毁
     40      NSLog(@"------2----self.weakSwitch=%p-------",self.weakSwitch);
     41     
     42     
     43     //strong强指针引用:创建完对象不会立即销毁,直到控制器销毁的时候,该变量才会被销毁
     44     self.strongSwitch = [[UISwitch alloc]init];
     45      NSLog(@"------3----self.strongSwitch=%p-------",self.strongSwitch);
     46     
     47     
     48     //3:将强引用的指针赋值给弱引用指针:self.weakSwitch对象不会被销毁
     49     /*
     50      1:因为[[UISwitch alloc]init]创建出一个对象后,默认有一个强引用,不会立即销毁,直到整个方法执行完毕后才会销毁。有一个强引用self.strongSwitch指向Switch对象,则Switch对象直到整个控制器销毁的时候才会被释放。
     51      2:self.weakSwitch = self.strongSwitch;将强引用对象赋值,其实也就是将强引用的指针赋值给弱引用,也就是两个指针指向同一块内存地址,但是两者区别,一个而是强引用,一个是弱引用。
     52      
     53      3:所以Switch对象分别有一个强引用指针和一个弱引用指针,为什么没有立即被销毁?是因为还有一个强指针引用switch对象
     54      
     55      */
     56     self.weakSwitch = self.strongSwitch;
     57     NSLog(@"-----4----- self.weakSwitch=%p-------", self.weakSwitch);
     58     
     59     //4:self.weakButton没有被销毁,
     60     
     61     /*
     62      1:打印5,6,7其中,57,self.weakButton没有被销毁,因为 UIButton *button = [[UIButton alloc]init] 创建出对象默认有一个强引用,只要该方法没有执行完,就不会被销毁,即使有若指针指向
     63      2:但是6中打印却被销毁,因为其为成员变量,出了该方法的作用域,没有了强指针指向了所以会被立即销毁
     64      
     65      3:打印5,6,7self.weakButton都没有被销毁,若将button添加到self.view上,则在addSubview时,view会对button有一个强引用,出了该方法的作用域,self.view依然有一个强引用指向button,所以对象不会被销毁,self.weakButton指针可以找到该内存地址,所以self.weakButton也不会被销毁
     66      4:[[UIButton alloc]init] 操作是向内存申请开辟一段内存空间,button为一个指针,指向这块内存地址,默认为强指针,若是局部变量,则出了方法的作用域,强指针引用消失,这块内存地址也就会被释放。弱指针指向的内存区域不存在,若指针也消失被释放 self.weakButton = button;此操作是将指针赋值给self.weakButton,则self.weakButton也指向同一块的内存空间,但是若指针,但是在该方法作用域内,这块内存空间一直被强指针引用,所以弱指针指向的这块内存空间依然可以被找到,弱指针也没有被释放。
     67      
     68      5:若是 __weak  UIButton *btn = [[UIButton alloc]init];或是 self.weakSwitch = [[UISwitch alloc]init];若是以上两种写法,则对象立即被释放。两者都是直接弱指针赋值,没有强引用,所以创建完毕后立即销毁
     69      */
     70     
     71     UIButton *button = [[UIButton alloc]init];
     72     self.weakButton = button;
     73     NSLog(@"----5---- self.weakButton=%p-------", self.weakButton);
     74     [self test];
     75     [self.view addSubview:button];
     76     
     77     
     78     /*
     79     5:关于xib中拖线时控件用weak修饰:因为viewcontroller对view有一个强引用,控件添加到view时,则view又对控件有一个强引用,当从控制器拖线时,显示为weak,因为view已经有一个强引用了,保证了控件不被销毁,其实strong也可以,因为销毁的顺序为,控制器先销毁,上面的view没有了强引用,也就被销毁,控件没有了view的强引用,和控制器的强引用则依然被销毁
     80      
     81      */
     82     
     83     
     84 }
     85 
     86 - (void)viewDidAppear:(BOOL)animated {
     87     
     88     [super viewDidAppear:animated];
     89       NSLog(@"----6---- self.weakButton=%p-------", self.weakButton);
     90 }
     91 
     92 
     93 
     94 - (void)test {
     95     
     96       NSLog(@"----7---- self.weakButton=%p-------", self.weakButton);
     97     
     98 }
     99 
    100 @end
  • 相关阅读:
    HTML5 跨文档消息传输
    Cordova 本地项目创建方法
    远程登录协议
    Linux mii-tool命令
    Linux ethtool命令
    Linux内核阅读相关
    C语言介绍
    Proc-fs 编程
    Linux syslog介绍
    Mpich编程
  • 原文地址:https://www.cnblogs.com/cqb-learner/p/5718072.html
Copyright © 2011-2022 走看看