您的位置:首页 > 移动开发 > IOS开发

关于iOS中的懒加载

2015-08-13 13:31 513 查看
懒加载,又称为延迟加载。说的通俗一点,就是在开发中,当程序中需要利用的资源时。在程序启动的时候不加载资源,只有在运行当需要一些资源时,再去加载这些资源。

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

下面举个例子:

1> 定义控件属性,注意:属性必须是strong的,示例代码如下:
</pre></p><pre class="code" name="code" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; margin-top: 10px; margin-bottom: 10px; padding: 8px 10px 8px 12px; font-family: Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace; border-width: 1px 1px 1px 5px; border-style: solid; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(108, 226, 108); background-color: rgb(241, 241, 241); line-height: 25px;"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(187, 44, 162);">@property<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> (</span>nonatomic<span style="font-variant-ligatures: no-common-ligatures; color: #000000">, </span>strong<span style="font-variant-ligatures: no-common-ligatures; color: #000000">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSMutableArray</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> *array;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Me
4000
nlo; color: rgb(187, 44, 162);">@property<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> (</span>nonatomic<span style="font-variant-ligatures: no-common-ligatures; color: #000000">, </span>strong<span style="font-variant-ligatures: no-common-ligatures; color: #000000">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa">UILabel</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> *label;</span></p>

2> 在属性的getter方法中实现懒加载,示例代码如下:
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(0, 132, 0);">/**<span style="font-family: 'Heiti SC Light';">数组的懒加载,</span>getter<span style="font-family: 'Heiti SC Light';">方法</span>*/</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(112, 61, 170);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">- (</span>NSMutableArray<span style="font-variant-ligatures: no-common-ligatures; color: #000000"> *)array {</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">if</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_array</span> == <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">nil</span>) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">        <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_array</span> = [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSMutableArray</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">array</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">    }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_array</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(0, 132, 0);">/**UI<span style="font-family: 'Heiti SC Light';">控件的懒加载</span>, getter<span style="font-family: 'Heiti SC Light';">方法</span>*/</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">- (<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">UILabel</span> *)label {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">if</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_label</span> == <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">nil</span>) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">        <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_label</span> = [[<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">UILabel</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">alloc</span>] <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">init</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">        <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_label</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">frame</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">CGRectMake</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">100</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">100</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">200</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">50</span>);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">        [<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">view</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">addSubview</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_label</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">    }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">    <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">_label</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">}</p>


如上面的代码,有一个_array属性,如果在程序的代码中,有多次访问_array属性,例如下面
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(0, 132, 0);">// <span style="font-family: 'Heiti SC Light';">第一次调用点语法</span>array = nil<span style="font-family: 'Heiti SC Light';">,会执行</span>_array = [NSMutableArray array];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(0, 132, 0);"><span style="color: rgb(51, 51, 51);">    [</span><span style="color: rgb(187, 44, 162);">self</span><span style="color: rgb(51, 51, 51);">.</span><span style="color: rgb(79, 129, 135);">array</span><span style="color: rgb(51, 51, 51);"> </span><span style="color: rgb(61, 29, 129);">addObject</span><span style="color: rgb(51, 51, 51);">:</span><span style="color: rgb(209, 47, 27);">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">第一个</span><span style="color: rgb(209, 47, 27);">"</span><span style="color: rgb(51, 51, 51);">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);"><span style="font-family: Menlo;">// </span>之后不管调用几次,都是直接返回<span style="font-family: Menlo;">array</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">    [<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">array</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">addObject</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">第二个</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">    [<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">array</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">addObject</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">第三个</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span>];</p>


懒加载的好处:

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

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

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

提醒:这是苹果公司提倡的做法。其实苹果公司做的IOS系统中很多地方都用到了懒加载的方式,比如控制器的View的创建。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  懒加载