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 it lazy let because 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 self inside 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

最新文章

  1. 速度极快的导出excel
  2. Nginx/Apache发大招
  3. Verilog学习笔记认识提升篇(一)...............时序的基本概念(待补充)
  4. 操作系统开发系列—13.d.多进程 ●
  5. 转:鏖战双十一-阿里直播平台面临的技术挑战(webSocket, 敏感词过滤等很不错)
  6. [gulp] gulp lint 忽略文件
  7. linux服务器wget无法成功解析域名及程序获取外网数据不稳定问题
  8. [amazonaccess 1]logistic.py 特征提取
  9. 关于CDC在非控件类中的使用
  10. windows搭建代理服务器
  11. Mybatis按顺序获取数据
  12. mac 查看某个文件夹下所有隐藏文件(夹)的大小
  13. java集合框架--List、Set、Map
  14. war和war exploded区别
  15. QSS网址
  16. Unity使用协程技术制作倒计时器
  17. 使用nvm-windows安装nodejs遇到的问题(转载)
  18. vue--获取监听获取radius的改变
  19. 计算机网络【3】—— IP地址分类与子网划分
  20. js对数字的校验

热门文章

  1. android网络类型之2G-3G切换
  2. Java反射获取class对象的三种方式,反射创建对象的两种方式
  3. 使用VS Code断点调试PHP
  4. 浅谈 Mysql
  5. Codeforces 1106F Lunar New Year and a Recursive Sequence (数学、线性代数、线性递推、数论、BSGS、扩展欧几里得算法)
  6. BZOJ——T 1053: [HAOI2007]反素数ant
  7. Spring注解@Repository、@Service、@Controller、@Component
  8. Keil5.15使用GCC编译器链接.a库文件
  9. Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.2
  10. UVA - 10061 How many zero's and how many digits ?