[IOS 开发] 懒加载 (延迟加载) 的基本方式,好处,代码示例


 

懒加载的好处:

1> 不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强

2> 每个属性的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合

3>只有当真正需要资源时,再去加载,节省了内存资源。

 

1.懒加载基本

 

我们知道iOS设备的内存有限,如果在程序在启动后就一次性加载将来会用到的所有资源,那么就有可能会耗尽iOS设备的内存。这些资源例如大量数据,图片,音频等等

懒加载——也称为延迟加载,说的通俗一点,就是在开发中,当程序中需要利用的资源时。在程序启动的时候不加载资源,只有在运行当需要一些资源时,再去加载这些资源,即在需要的时候才加载(效率低,占用内存小),所谓懒加载,写的是其get方法.

 

提醒:这是苹果公司提倡的做法。其实苹果公司做的IOS系统中很多地方都用到了懒加载的方式,比如控制器的View的创建。

注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化

 

2.使用懒加载的好处:

(1)不必将创建对象的代码全部写在viewDidLoad方法中,代码的可读性更强

(2)每个控件的getter方法中分别负责各自的实例化处理,代码彼此之间的独立性强,松耦合

 

3.代码示例

 

代码示例1:

 

1> 定义控件属性,注意:属性必须是strong的,示例代码如下:

@property (nonatomic, strong) NSArray *imageList;

 

2> 在属性的getter方法中实现懒加载,示例代码如下:

// 懒加载-在需要的时候,在实例化加载到内存中
- (NSArray *)imageList
{
    // 只有第一次调用getter方法时,为空,此时实例化并建立数组
    if (_imageList == nil) {
        // File表示从文件的完整路径加载文件
        NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageData" ofType:@"plist"];
        NSLog(@"%@", path);
        
        _imageList = [NSArray arrayWithContentsOfFile:path];
    }
    
    return _imageList;
}

 

如上面的代码,有一个_imageList属性,如果在程序的代码中,有多次访问_imageList属性,例如下面

self.imageList ;

self.imageList ;

self.imageList ;

 

虽然访问了3次_imageList 属性,但是当第一次访问了imageList属相,imageList数组就不为空,
当第二次访问imageList 时  imageList != nil;程序就不会执行下面的代码

NSString *path = [[NSBundle mainBundle] pathForResource:@"ImageData" ofType:@"plist"];
        NSLog(@"%@", path);
        
        _imageList = [NSArray arrayWithContentsOfFile:path];

 

就不会再次在PList文件中加载数据了。

 

 

===========================

 

 

代码示例2:

//
//  YYViewController.m
//  03-图片浏览器初步
//
//  Created by apple on 14-5-21.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

#define POTOIMGW    200
#define POTOIMGH    300
#define POTOIMGX    60
#define  POTOIMGY    50

@interface YYViewController ()

@property(nonatomic,strong)UILabel *firstlab;
@property(nonatomic,strong)UILabel *lastlab;
@property(nonatomic,strong)UIImageView *icon;
@property(nonatomic,strong)UIButton *leftbtn;
@property(nonatomic,strong)UIButton *rightbtn;
@property(nonatomic,strong)NSArray *array;
@property(nonatomic ,assign)int i;
-(void)change;
@end



@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self change];
}

-(void)change
{
    [self.firstlab setText:[NSString stringWithFormat:@"%d/5",self.i+1]];
    //先get再set
    
    self.icon.image=[UIImage imageNamed:self.array[self.i][@"name"]];
    self.lastlab.text=self.array[self.i][@"desc"];
  
    self.leftbtn.enabled=(self.i!=0);
    self.rightbtn.enabled=(self.i!=4);
}

//延迟加载
/**1.图片的序号标签*/
-(UILabel *)firstlab
{
    //判断是否已经有了,若没有,则进行实例化
    if (!_firstlab) {
        _firstlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)];
        [_firstlab setTextAlignment:NSTextAlignmentCenter];
        [self.view addSubview:_firstlab];
    }
    return _firstlab;
}

/**2.图片控件的延迟加载*/
-(UIImageView *)icon
{
     //判断是否已经有了,若没有,则进行实例化
    if (!_icon) {
        _icon=[[UIImageView alloc]initWithFrame:CGRectMake(POTOIMGX, POTOIMGY, POTOIMGW, POTOIMGH)];
        UIImage *image=[UIImage imageNamed:@"biaoqingdi"];
        _icon.image=image;
        [self.view addSubview:_icon];
    }
    return _icon;
}

/**3.描述控件的延迟加载*/
-(UILabel *)lastlab
{
     //判断是否已经有了,若没有,则进行实例化
    if (!_lastlab) {
        _lastlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 400, 300, 30)];
        [_lastlab setTextAlignment:NSTextAlignmentCenter];
        [self.view addSubview:_lastlab];
    }
    return _lastlab;
}

/**4.左键按钮的延迟加载*/
-(UIButton *)leftbtn
{
     //判断是否已经有了,若没有,则进行实例化
    if (!_leftbtn) {
        _leftbtn=[UIButton buttonWithType:UIButtonTypeCustom];
        _leftbtn.frame=CGRectMake(0, self.view.center.y, 40, 40);
        [_leftbtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
        [_leftbtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
        [self.view addSubview:_leftbtn];
        [_leftbtn addTarget:self action:@selector(leftclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftbtn;

}

/**5.右键按钮的延迟加载*/
-(UIButton *)rightbtn
{
    if (!_rightbtn) {
        _rightbtn=[UIButton buttonWithType:UIButtonTypeCustom];
        _rightbtn.frame=CGRectMake(POTOIMGX+POTOIMGW+10, self.view.center.y, 40, 40);
        [_rightbtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
        [_rightbtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
        [self.view addSubview:_rightbtn];
        [_rightbtn addTarget:self action:@selector(rightclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightbtn;
}

//array的get方法
-(NSArray *)array
{
    if (_array==nil) {
        NSString *path=[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
        _array=[[NSArray alloc]initWithContentsOfFile:path];
    }
    return _array;
}

-(void)rightclick:(UIButton *)btn
{
    self.i++;
    [self change];
}

-(void)leftclick:(UIButton *)btn
{
    self.i--;
    [self change];
}

@end

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM