Skip to content

Instantly share code, notes, and snippets.

@siracusa
Last active June 8, 2023 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siracusa/fa2457bbfef11658751cab6a6c19cc6f to your computer and use it in GitHub Desktop.
Save siracusa/fa2457bbfef11658751cab6a6c19cc6f to your computer and use it in GitHub Desktop.
Possible workarounds for lazy properties in an @observable class
import Cocoa
import Observation
@Observable
class User {
@ObservationIgnored lazy var id : Int = {
return Int.random(in: 1...100)
}()
}
//
// …or…
//
@Observable
public class User {
var idStorage : Int? = nil
var id : Int {
get {
if idStorage == nil {
idStorage = Int.random(in: 1...100)
}
return idStorage!
}
set {
idStorage = newValue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment