• IOS-lazyload思想


    lazyload即懒加载,理解为按需加载,就是在需要时进行加载,避免提前加载和加载过多,消耗很多内存

    通常对NSARRAY,nsmutablearray等数组进行初始化时使用;

    例如:

    @interface ViewController ()

    @property (nonatomic ,retain)NSMutableData *bufferData;//声明属性

     @end

     

    @implementation ZYViewController

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        //不在viewDidLoad中进行初始化

     

      [self stringAndData];//

    }

    //重写属性的get方法

    - (NSMutableData *)bufferData

    {

        //  !!! 注意全局变量别忘记初始化,否则她只是一个空指针。

       //如果指针为空的话,对其进行初始化

        if (!_bufferData)

        {

            _bufferData = [[NSMutableData alloc] init];

         NSLog(@"-----");

        }

        return _bufferData;

    }

     

    -(void)stringAndData{

        NSString *str1=@"as";

        NSData *data=[str1 dataUsingEncoding:NSUTF8StringEncoding];

        [self.bufferData appendData:data];

        NSLog(@"%@",_bufferData);

        NSString *str2=[[NSString alloc]initWithData:self.bufferData encoding:NSUTF8StringEncoding];

        NSLog(@"%@",str2);

    }

    @end

    采用self.Data时会调用访问器的get方法;

    直接用_Data进行赋值则不会访问get,set方法;

    输出结果:

     

     

     

     

  • 相关阅读:
    微信小程序の模板
    微信小程序の条件渲染
    微信小程序のwxml列表渲染
    769. Max Chunks To Make Sorted
    766. Toeplitz Matrix
    747. Largest Number At Least Twice of Others
    746. Min Cost Climbing Stairs
    729. My Calendar I
    724. Find Pivot Index
    718. Maximum Length of Repeated Subarray
  • 原文地址:https://www.cnblogs.com/lpjdbk/p/4719157.html
Copyright © 2020-2023  润新知