如果全局属性总是惰性计算,而局部属性从不惰性计算,那么惰性修饰符会修改什么?

If global properties are always computed lazily, and local properties are never, what does the lazy modifier modify?

Swift 编程语言 (Swift 2.1) 第 248 页的注释解释如下:

Global constants and variables are always computed lazily, in a similar manner to Lazy Stored Properties. Unlike lazy stored properties, global constants and variables do not need to be marked with the lazy modifier.

Local constants and variables are never computed lazily.

摘自:Apple Inc.“Swift 编程语言 (Swift 2.1)。” https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11

除了 lazy 修饰符会产生影响的全局属性和局部值之外,还有其他类型的常量或变量吗?

"Local constants and variables" 在提供的摘录中引用局部作用域常量和变量,就像在函数的局部变量中一样。它们不引用对象的属性,如果用 lazy 关键字标记,它们可以是惰性的。

//global, declared outside of a class/struct
//error is "Lazy is only valid for members of a struct or class
lazy var label: UILabel = {
    var tempLabel: UILabel = UILabel()
    tempLabel.text = "hi"
    return tempLabel
}()

class SomeClass : NSObject {
    //non-lazy instance property
    var x = 3

    //lazy instance property
    lazy var label: UILabel = {
        var tempLabel: UILabel = UILabel()
        tempLabel.text = "hi"
        return tempLabel
    }()


    func doStuff() {
        //error is "Lazy is only valid for members of a struct or class
        lazy var label: UILabel = {
            var tempLabel: UILabel = UILabel()
            tempLabel.text = "hi"
            return tempLabel
        }()
    }
}

这里的全局指的是在class括号{ }声明的变量和常量 例如

var anotherProerty : AClass() //This will be lazy by default 

Class Abc 
    {
        var aProperty : AClass()  // This will NOT be lazy by default
        //however it can be lazy by declaring it with 'lazy' KEYWORD

        func abc()
        {
            var another: AClass()  //This cannot be lazy 
        }


    }