• iOS基础之懒加载


      什么是懒加载,即延迟加载,在你需要的时候才加载,也就是说在不需要的时候是不会加载的,减小了占用内存。当然在使用懒加载的时候要注意先加一个判断去判断有无。

      为什么要使用懒加载呢?除了上述说的能够减小占用内存,还有就是不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强,每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合。

      代码演示:

      

    #import "RootViewController.h"
    
    @interface RootViewController ()
    @property(nonatomic,retain)UILabel *label;
    @property (nonatomic,retain)UITextField *textField;
    @end
    
    @implementation RootViewController
    //懒加载
    - (UILabel *)label{
        if (_label == nil) {
            _label = [[UILabel alloc]initWithFrame:CGRectMake(200, 100, 100, 20)];
            
        }return _label;
    }
    - (UITextField *)textField{
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 40)];
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        return _textField;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
  • 相关阅读:
    python 之 sys.argv 外部传参动态匹配内部字典
    我是如何打败拖延症的
    图解Docker容器和镜像
    docker1-安装和使用
    python操作RabbitMQ
    Python操作 Memcache、Redis
    python队列、线程、进程、协程
    项目协作与工作流程规范
    python26:自定义form表单验证
    s11d27 算法
  • 原文地址:https://www.cnblogs.com/16-jkd/p/5205719.html
Copyright © 2020-2023  润新知