what do parenthesis do after lazy var definition?

别来无恙 提交于 2019-12-19 10:26:11

问题


I am analyzing analyzing some third party code and there is a "lazy" var statement that looks like this, and I would like to understand what the parenthesis are doing after the "computed property" curly braces:

lazy var defaults:NSUserDefaults = {
    return .standardUserDefaults()
}()

The "return .standardUserDefaults()" is returning the NSUserDefaults instance object, so why add a () after the right curly brace?

thanks


回答1:


It means that its a block that is executed the first time defaults is accessed. Without the () it means the defaults is a block type of variable of type () -> NSUserDefaults. When you add () it means it's just NSUserDefaults which is returned by the block executed at the time of access.



来源:https://stackoverflow.com/questions/35237786/what-do-parenthesis-do-after-lazy-var-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!