Search code examples
swiftparenthesescomputed-properties

what do parenthesis do after lazy var definition?


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


Solution

  • 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.