Risposta breve: no.
Prova questo semplice esempio, in qualche classe o un metodo di tuo:
lazy var myLazyVar: Int = {
return 1
}() {
willSet {
print("About to set lazy var!")
}
}
Questo vi dà il seguente errore di compilazione:
Lazy properties may not have observers.
Per quanto riguarda la dichiarazione let
in l'altra risposta: non è necessaria la variabile lazy solo "Consenti costanti con inizializzazione ritardata". Si consideri il seguente esempio:
struct MyStruct {
var myInt = 1
mutating func increaseMyInt() {
myInt += 1
}
lazy var myLazyVar: Int = {
return self.myInt
}()
}
var a = MyStruct()
print(a.myLazyVar) // 1
a.increaseMyInt()
print(a.myLazyVar) // 1: "initialiser" only called once, OK
a.myLazyVar += 1
print(a.myLazyVar) // 2: however we can still mutate the value
// directly if we so wishes
cosa è 'ar' in' for p in ar'? – dfri
Cambiato, niente a che fare con la domanda in realtà. Grazie. –