What are lazy variables?
Written by Paul Hudson @twostraws
It's very common in iOS to want to create complex objects only when you need them, largely because with limited computing power at your disposal you need to avoid doing expensive work unless it's really needed.
Swift has a mechanism built right into the language that enables just-in-time calculation of expensive work, and it is called a lazy variable. These variables are created using a function you specify only when that variable is first requested. If it's never requested, the function is never run, so it does help save processing time.
I don't want to produce a complicated example because that would rather defy the point, so instead I've built a simple (if silly!) one: imagine you want to calculate a person's age using the Fibonacci sequence. This sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on – each number is calculated by adding the previous two numbers in the sequence. So if someone was aged 8, their Fibonacci sequence age would be 21, because that's at position 8 in the sequence.
I chose this because the most common pedagogical way to teach the Fibonacci sequence is using a function like this one:
That function calls itself, which makes it a recursive function, and actually it's quite slow. If you try to calculate the Fibonacci value of something over, say, 21, expect it to be slow in a playground!
Anyway, we want to create a Person struct that has an age property and a fibonacciAge property, but we don't want that second one to be evaluated unless it's actually used. So, create this struct now:
There are five important things to note in that code:
- The lazy property is marked as
lazy var. You can't make itlazy letbecause lazy properties must always be variables. - Because the actual value is created by evaluation, you need to declare its data type up front. In the case of the code above, that means declaring the property as
Int. - Once you've set your data type, you need to use an open brace ("{") to start your block of code, then "}" to finish.
- You need to use
selfinside the function. In fact, if you're using a class rather than a structure, you should also declare[unowned self]inside your function so that you don't create a strong reference cycle. - You need to end your lazy property with
(), because what you're actually doing is making a call to the function you just created.
Once that code is written, you can use it like this:
Remember, the point of lazy properties is that they are computed only when they are first needed, after which their value is saved. This means if you create 1000 singers and never touch their fibonacciOfAgeproperty, your code will be lightning fast because that lazy work is never done.
Available from iOS 7.0
https://www.hackingwithswift.com/example-code/language/what-are-lazy-variables
What are lazy variables?的更多相关文章
- Beginning Scala study note(2) Basics of Scala
1. Variables (1) Three ways to define variables: 1) val refers to define an immutable variable; scal ...
- Effective Java 71 Use lazy initialization judiciously
Lazy initialization - It decreases the cost of initializing a class or creating an instance, at the ...
- lazy instructor
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- 数据结构——POJ 1686 Lazy Math Instructor 栈的应用
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...
- Lazy Math Instructor
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3721 Accepted: 1290 Description A m ...
- Summary: Deep Copy vs. Shallow Copy vs. Lazy Copy
Object copy An object copy is an action in computing where a data object has its attributes copied t ...
- Lazy的SDL教程 翻译----Lesson 22 Timing
原文:http://lazyfoo.net/tutorials/SDL/22_timing/index.php Timing 计时 Last Updated 3/10/14 Another impor ...
- 代码的坏味道(15)——冗余类(Lazy Class)
坏味道--冗余类(Lazy Class) 特征 理解和维护类总是费时费力的.如果一个类不值得你花费精力,它就应该被删除. 问题原因 也许一个类的初始设计是一个功能完全的类,然而随着代码的变迁,变得没什 ...
随机推荐
- lamp平台搭建论坛网站(Discuz论坛)
1. 安装Apache 1) 安装apr [root@www lamp]# yum install zlib-devel gcc gcc-c++ openssl-devel pcre-devel -y ...
- Linux之浅谈VIM常见用法及原理图
本次归纳以强大的VIM文本处理工具常见用法去展开论述. 文本编辑种类: 行编辑器:sed 全屏编辑器:nano,vi vim - Vi改进 其他编辑器: ...
- 编码的由来,ASCII编码,和字节的形成
一.编码的由来 计算机只能计算和识别二进制,必须让计算机识别文字,才能和计算机进行交互,彼此也才能通过计算机通信. 由此,有了ASCII编码的诞生,它起始于50年代后期,在1967年定案,是最初美国国 ...
- ik分词器各版本下载地址
ik分词器各个版本下载地址: https://github.com/medcl/elasticsearch-analysis-ik/releases
- luogu P4238 多项式求逆 (模板题、FFT)
手动博客搬家: 本文发表于20181125 13:21:46, 原地址https://blog.csdn.net/suncongbo/article/details/84485718 题目链接: ht ...
- Docker 入门,镜像,安装, 数据,网络,配置
https://yeasy.gitbooks.io/docker_practice/basic_concept/image.html
- EntityFramework:状态变化与方法的关系[转载]
原文地址 一.约定 OnModelCreating 有一些限制需要注意,例如: 1.表名不支持使用标签进行标注 2.最小长度在 OnModelCreating 中不支持 3.正则表达式在 OnMode ...
- [bzoj2466][中山市选2009]树_树形dp
树 bzoj-2466 中山市选-2009 题目大意:给定一棵树,每一个点有一个按钮和一个灯泡.如果按下一个点的按钮那么和这个点直接相连的点包括这个点的灯泡的状态会改变.如果是点亮就会变成熄灭,如果 ...
- Java 代理学习笔记
http://blog.csdn.net/mr_seaturtle_/article/details/52686516
- apple 团队电话
back 苹果电话:400 670 18552 这个是国内能打通的