Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

stackoverflow community.

This is my first post/question on here.
Newbie here. I’m trying to make a plantApp using SwiftUI, but currently running into an error.

I’m trying to figure out why I keep getting the following error
"Cannot use instance member ‘selectedWateredDate’ within property initializer; property initializers run before ‘self’ is available" for my "dateInterval" variable.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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


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

>Solution :

Change the let property to a computed variable.

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading