懒加载的好处:

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+]];
//先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!=);
self.rightbtn.enabled=(self.i!=);
} //延迟加载
/**1.图片的序号标签*/
-(UILabel *)firstlab
{
//判断是否已经有了,若没有,则进行实例化
if (!_firstlab) {
_firstlab=[[UILabel alloc]initWithFrame:CGRectMake(, , , )];
[_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(, , , )];
[_lastlab setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:_lastlab];
}
return _lastlab;
} /**4.左键按钮的延迟加载*/
-(UIButton *)leftbtn
{
//判断是否已经有了,若没有,则进行实例化
if (!_leftbtn) {
_leftbtn=[UIButton buttonWithType:UIButtonTypeCustom];
_leftbtn.frame=CGRectMake(, self.view.center.y, , );
[_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+, self.view.center.y, , );
[_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
 

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

  1. 解决hibernate中的懒加载(延迟加载)问题

    解决hibernate中的懒加载(延迟加载)问题   我们在开发的时候经常会遇到延迟加载问题,在实体映射时,多对一和多对多中,多的一样的属性默认是lazy="true"(即,默认是 ...

  2. iOS中懒加载

    1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化 ...

  3. iOS wkwebview懒加载中遇到的问题

    这是我遇到的问题,也许是个例,就算狗血了点吧 需求: 当前界面(mainVC)响应点击事件,传值给webviewController(webVC)其中包含网址,此时如果在webVC中对wkwebvie ...

  4. iOS开发-UIWebView加载本地和网络数据

    UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档,关于浏览网页榜样可以参考UC,手机必备浏览器,至于文档浏览的手机很多图书阅读软件,UIWebView是一个混合体,具体的功能控件内置 ...

  5. vuex存取token,http简单封装、模拟登入权限校验操作、路由懒加载的几种方式、vue单页设置title

    1.config index.js下面的跨域代理设置: proxyTable: { '/api': { target: 'http://xxxx', //要访问的后端接口 changeOrigin: ...

  6. Webpack实现路由懒加载的三种方式

    原文指路:https://blog.csdn.net/qq_37540004/article/details/78727063 第一种: 引入方式(正常引入): const router = new ...

  7. vue+webpack 实现懒加载的三种方式

    第一种: 引入方式 就是正常的路由引入方式 const router = new Router({ routes: [ { path: '/hyh', component: hyh, name: 'h ...

  8. iOS开发图片加载的内存问题及优化方案

    原创作者:Magic-Unique 原文地址:https://github.com/Magic-Unique/HXImage猿吧 - 资源共享论坛: http://www.coderbar.cn 做最 ...

  9. ios开发中加载的image无法显示

    昨天遇到一个较奇葩的问题,imageName加载的图片显示不出来,网上查了好多资料还是没找到解决的方法: 之前图片是放在项目中SupportingFiles文件下的,怎么加载都能显示图片,于是将图片拿 ...

随机推荐

  1. Java Web学习路线

    2016-08-22的早上,本是一个很平静的早上,坐在去往公司的公交车上想到了很多之前上学时的点点滴滴,回想起来还真的是耐人寻味啊,当初青春的懵懂,当初的冲动,当初的做事不考虑后果! 也正是这耐人寻味 ...

  2. three.js初涉略(一)

    <!-- 最近要研究一下webgl,发现了webgl中文网(http://www.hewebgl.com/article/articledir/1).边研究教程边做下记录 --> thre ...

  3. web项目开发规范整理总结

    一.类.函数.变量名命名: 1.定义类时,全部拼音的首字母必须大写:如Person,ClassDemo:(帕斯卡命名法):也可以用带下斜杆的匈牙利命名法进行命名,如    head_navigatio ...

  4. 不写完不回家的TreeSet

    TreeSet详解 继承架构图: |——SortedSet接口——TreeSet实现类 Set接口——|——HashSet实现类                  |——LinkedHashSet实现 ...

  5. SuiteScript > Apply script to Assembly and Kit

    Path: Customization > Scripting > Scripts > New Limitation: Client script can't apply to As ...

  6. 据说年薪30万的Android程序员必须知道的帖子

    Android中国开发精英 目前包括: Android开源项目第一篇--个性化控件(View)篇       包括ListView.ActionBar.Menu.ViewPager.Gallery.G ...

  7. 扩展progress_timer的计时精度

    progress对外输出精度只有小数点后两位(这点可以运行上节程序进行验证),即精确到0.01秒. 我们使用模板技术仿造一个progress_timer编写一个新类:new_progress_time ...

  8. SQL 个版本下载地址

    备用: SQL Server 2016简体中文企业版 文件名:cn_sql_server_2016_enterprise_x64_dvd_8699450.iso 64位下载地址:ed2k://|fil ...

  9. Cocos2d-x 核心概念 - 导演(Director)

    导演类(Director) 用于管理场景对象,采用的是单例模式(单例模式能保存一致的配置信息,方便管理场景对象) 获得导演实例的语句如下 local director = cc.Director:ge ...

  10. rxjava源码分析

    RXjava响应式编程 此文作者大暴雨原创,转载请注明出处. 如果线程的知识不是很丰富,请先查看     rxjava源码中的线程知识  一文 rxjava总结就是:异步实现主要是通过扩展观察者模式 ...