Xcode error: Cannot use instance member 'selectedWateredDate' within property initializer; property initializers run before 'self' is available

Xcode error: Cannot use instance member 'selectedWateredDate' within property initializer; property initializers run before 'self' is available

我想弄清楚为什么我在使用 SwiftUI 时总是收到以下错误:

Cannot use instance member 'selectedWateredDate' within property initializer; property initializers run before 'self' is available

我的“dateInterval”变量。

@State private var selectedWateredDate = Date()
@State private var waterMeAgainIn: Date = Date().addingTimeInterval(7 * 86400)


let dateInterval = DateInterval(start: selectedWateredDate, end: waterMeAgainIn)

将 let 属性 更改为计算变量。

var dateInterval : DateInterval { DateInterval(start: selectedWateredDate, end: waterMeAgainIn)}