#27: Lazy Initialization
Question
// Swift Quiz
— objc.io (@objcio) August 21, 2019
var count = 0
var book: String = {
count += 1
return "Advanced Swift"
}()
func add(_ completion: @autoclosure () -> String) {
count += 1
}
add(book)
count
// What‘s the value of count?
Solution
Solution: it depends.
— objc.io (@objcio) August 22, 2019
In a Playground (or a command-line app), this will print "2".
In a Mac app or UIKit app, global variables are computed lazily, and therefore, it will print "1".https://t.co/s7500CWmAg