Lazy Stored Properties--无括号时为匿名函数
第一次使用的时候进行计算和初始化,后面的引用不在进行计算。
A 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--无括号时为匿名函数的更多相关文章
- C#绑定事件时使用匿名函数
当使用一些临时的函数 可以预知这些函数基本不会被复用时 可以使用匿名函数简化代码 public static void startCoupons() { //绑定一些事件 userGetCoupon ...
- 无参数的lambda匿名函数
lambda 语法: lambda [arg1[,arg2,arg3....argN]]:expression 1.单个参数的: g = lambda x:x*2 print g(3) 结果是6 2. ...
- javascript 匿名函数的理解,js括号中括function 如(function(){})
代码如下: (function(){ //这里忽略jQuery所有实现 })(); (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也 ...
- 自执行的匿名函数!function()
最近有空可以让我静下心来看看各种代码,function与感叹号的频繁出现,让我回想起2个月前我回杭州最后参加团队会议的时候,@西子剑影抛出的一样的问题:如果在function之前加上感叹号 (!) 会 ...
- JS自执行匿名函数
常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号内为匿 ...
- jquery的匿名函数研究
jQuery片段: ? 1 2 3 ( function (){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在 ...
- [从jQuery看JavaScript]-匿名函数与闭包(Anonymous Function and Closure)【转】
(function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的第一眼,我就迷糊了.为什么只有 ...
- JS 匿名函数 自执行
其实就是将函数直接做为表达调用,使用括号包裹定义函数体,解析器将会以函数表达式的方式去调用定义函数. 常见格式:(function() { /* code */ })(); 解释:包围函数(funct ...
- python匿名函数
文章导读: 以前自己一直没搞明白Python中的匿名函数,现在拿这个问题基本上搞明白了,拿自己的理解整成一篇文章,附带大量例子,让其更加好理解. 在编程语言中,函数的应用: 1. 代码块重复,这时候必 ...
随机推荐
- Bamboo Django Celery定时任务和时间设置
1.Celery加入定时任务 Celery除了可以异步执行任务之外,还可以定时执行任务.在实例代码的基础上写个测试方法: 1 #coding:utf-8 2 from celery.task.sche ...
- Hexo系列(二) 配置文件详解
Hexo 是一款优秀的博客框架,在使用 Hexo 搭建一个属于自己的博客网站后,我们还需要对其进行配置,使得 Hexo 更能满足自己的需求 这里所说的配置文件,是位于站点根目录下的 _config.y ...
- 32.es读请示内部分发原理
当客户端发送一次读请求时,大致会经过以下几个步骤 1.客户端发送一个请求过去,es的一个node接收到这个请求(随机的node),这个node就被es内部分配成coordinating node(协调 ...
- 多层下firebird自增长字段的处理
使用unidac可以解决自增长字段处理的问题. 对于多层就是一个比较烦人的事情,解决办法:cliendataset中自增长字段的处理:id:自增长字段.在client中的处理方法:clientdata ...
- 【Codeforces 639B】Bear and Forgotten Tree 3
[链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...
- 【codeforces 793C】Mice problem
[题目链接]:http://codeforces.com/contest/793/problem/C [题意] 给你每个点x轴移动速度,y轴移动速度; 问你有没有某个时刻,所有的点都"严格& ...
- html绑定点击事件传值
1.方法1function radioSelect(event){ var valueRadio=event.value; if(valueRadio =='目的口岸'){ return '目的口岸' ...
- hdu 2121无根最小树形图要建一个虚拟节点
#include<stdio.h> #include<string.h> #define inf 999999999 #define N 1100 struct node { ...
- csu1395模拟
#include<stdio.h> #include<string.h> #define N 10 char s[N][N][N]={{"***",&qu ...
- Bootstrap的js插件之警告框(alert.js)
data-dismiss="alert"--为关闭button加入该属性能够使其自己主动为警告框赋予关闭功能. .fade .in--为警告框在关闭时加入动画效果. 很多其它细节參 ...