I have an if statement in my SwiftUI view VStack to toggle an integer which stops a SQLite database from being created more than once, but I'm given numerous errors, stopping the code from building. The variable (appInfo.toggleswitch is an integer set to 0 by default in a class, which I subscribe to in this view using a environment object)
if appInfo.toggleswitch == 0 {
let db = DBHelper()
$appInfo.toggleswitch = 1 }
You cannot write code like this in a View. SwiftUI only render UI. I would do something like this:
Toggle(isOn: $appInfo.toggleswitch.onChange(myFunction)) {
YourView()
}
func myFunction() {
let db = DBHelper()
appInfo.toggleswitch = 1
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.