zoukankan      html  css  js  c++  java
  • strong和weak的区别

    简单的说:strong是强引用,weak是弱引用。

    关于二者实际应用中的详细区别,可查看:http://blog.csdn.net/q199109106q/article/details/8565017

    代码示例:

           NSString *string = @"zhongguo";

        

        textField1 = [[UITextField alloc] init];

        textField1.frame = CGRectMake(10, 100, 100, 50);

        [self.view addSubview:textField1];

        

        textField2 = [[UITextField alloc] init];

        textField2.frame = CGRectMake(10, 200, 100, 50);

        [self.view addSubview:textField2];

        

        //strong

        textField1.text = string;

        NSString *name1 = textField1.text;

        

        NSLog(@"textField1 ---> %@",textField1.text);//textField1 ---> zhongguo

        NSLog(@"name1 ---> %@",name1);//name1 ---> zhongguo

        

        textField1.text = @"textField1 ---> 01";

        

        NSLog(@"textField1 ---> %@",textField1.text);//textField1 ---> textField1 ---> 01

        NSLog(@"name1 ---> %@",name1);//name1 ---> zhongguo

        

        name1 = @"name1 ---> 01";

        

        NSLog(@"textField1 ---> %@",textField1.text);//textField1 ---> textField1 ---> 01

        NSLog(@"name1 ---> %@",name1);//name1 ---> name1 ---> 01


        

        NSLog(@" ");

        

        //weak

        textField2.text = string;

        __weak NSString *name2 = textField2.text;

        

        NSLog(@"textField2 ---> %@",textField2.text);//textField2 ---> zhongguo

        NSLog(@"name2 ---> %@",name2);//name2 ---> (null)

        

        textField2.text = @"textField2 ---> 01";


        NSLog(@"textField2 ---> %@",textField2.text);//textField2 ---> textField2 ---> 01

        NSLog(@"name2 ---> %@",name2);//name2 ---> (null)

  • 相关阅读:
    阅读Unity官方技术blog笔记
    设计模式-具有Model-View-ViewModel设计模式的WPF应用
    关于PureMVC的一点想法
    管窥WOWUIInterface代码
    Lua 5.4引入的新变动
    Unity手册SpriteAtlasManager翻译
    2D向量的数学计算
    提前编译(AOT编译)Wiki翻译
    Lua5.3手册标准库拾遗
    C#异步语法糖的苦与甜
  • 原文地址:https://www.cnblogs.com/NINIiOS/p/4897231.html
Copyright © 2011-2022 走看看