第一次使用的时候进行计算和初始化,后面的引用不在进行计算。

lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration.

NOTE

You must always declare a lazy property as a variable (with the var keyword), because its initial value might not be retrieved until after instance initialization completes. Constant properties must always have a value before initialization completes, and therefore cannot be declared as lazy.

Lazy properties are useful when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete. Lazy properties are also useful when the initial value for a property requires complex or computationally expensive setup that should not be performed unless or until it is needed.

class Lazytest{

var xx = 1

lazy var ee:String = {

return "\(self.xx)"

}()

lazy var ff = {

return "\(self.xx)"

}

lazy var gg = {para in

return para + "\(self.xx)"

}

}

有无括号的区别。无括号时为匿名函数

ee.storage   String? "1" some

ff.storage    (() -> String)? nil none

gg.storage   ((String) -> String)? nil none

(lldb) po px.ff()

"4"

(lldb) po px.gg("aa")

"aa4"

Lazy Stored Properties--无括号时为匿名函数的更多相关文章

  1. C#绑定事件时使用匿名函数

    当使用一些临时的函数 可以预知这些函数基本不会被复用时  可以使用匿名函数简化代码 public static void startCoupons() { //绑定一些事件 userGetCoupon ...

  2. 无参数的lambda匿名函数

    lambda 语法: lambda [arg1[,arg2,arg3....argN]]:expression 1.单个参数的: g = lambda x:x*2 print g(3) 结果是6 2. ...

  3. javascript 匿名函数的理解,js括号中括function 如(function(){})

    代码如下: (function(){  //这里忽略jQuery所有实现  })();  (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也 ...

  4. 自执行的匿名函数!function()

    最近有空可以让我静下心来看看各种代码,function与感叹号的频繁出现,让我回想起2个月前我回杭州最后参加团队会议的时候,@西子剑影抛出的一样的问题:如果在function之前加上感叹号 (!) 会 ...

  5. JS自执行匿名函数

    常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号内为匿 ...

  6. jquery的匿名函数研究

    jQuery片段: ? 1 2 3 ( function (){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在 ...

  7. [从jQuery看JavaScript]-匿名函数与闭包(Anonymous Function and Closure)【转】

    (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的第一眼,我就迷糊了.为什么只有 ...

  8. JS 匿名函数 自执行

    其实就是将函数直接做为表达调用,使用括号包裹定义函数体,解析器将会以函数表达式的方式去调用定义函数. 常见格式:(function() { /* code */ })(); 解释:包围函数(funct ...

  9. python匿名函数

    文章导读: 以前自己一直没搞明白Python中的匿名函数,现在拿这个问题基本上搞明白了,拿自己的理解整成一篇文章,附带大量例子,让其更加好理解. 在编程语言中,函数的应用: 1. 代码块重复,这时候必 ...

随机推荐

  1. 获取元素属性中的[x] 标签: javascript 2016-12-24 22:35 105人阅读 评论(0)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  2. Python SQLAlchemy ORM示例

    SQLAlchemy的是Python的SQL工具包和对象关系映射,给应用程序开发者提供SQL的强大功能和灵活性. 安装 pip install mysql-python pip install sql ...

  3. (23)Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】

    [Spring Boot 系列博客] )前言[从零开始学Spring Boot] : http://412887952-qq-com.iteye.com/blog/2291496 )spring bo ...

  4. dubbo-源码阅读之javaspi&javasist简单使用

    dubbo可扩展的点的类的对象创建 都是用类似javaspi和javasist的思想来做的.所以看后面代码 先熟悉一下java的SPI和javasist的使用 如ServicesConfig的代码 p ...

  5. java陷阱之Array.asList

    List<Integer> numbers= Arrays.asList(new Integer[] {1,2,3}); numbers.add(3); 运行这段代码会抛出 java.la ...

  6. [Unit Testing] Set the timeout of a Test in Mocha

    Mocha uses a default timeout of 2000 ms. However, if for some reason that does not work for your use ...

  7. Android学习笔记(8):ViewGroup类

    A ViewGroup is a special view that can contain other views (called children.) The view group is the  ...

  8. 基于ZF中的.htaccess文件学习Apache的Rewrite语法

    首先我们看一下这个.htaccess文件: RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST ...

  9. Spring+EhCache缓存实例(具体解说+源代码下载)

    一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有高速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...

  10. luogu2833 等式

    题目大意 给出\(a,b,c,x_1,x_2,y_1,y_2\),求满足\(ax+by+c=0\),且\(x\in[x1,x2],y\in [y1,y2]\)的整数解有多少对. 题解 用扩展欧几里得算 ...