|
Hi so my conversion app is working as intended (almost!) I would like to pop an alert when a valid Double is not provided as well as setting the default value to 0. I have almost achieved this but I believe I am changing my showingAlert variable to true in the wrong place. I get the warning:
Can anyone help?
|
|
Your call to calculate() changes the value of showingAlert, but calculate() is being called inside body{} so SwiftUI can't determine how to render the View reliably. showingAlert needs to be set outside the View itself, in a modifier of some sort or perhaps a button. Hopefully someone more experienced can offer a specific solution! |
|
Actually, there is a solution on how to fix this on this site :) https://www.hackingwithswift.com/quick-start/swiftui/how-to-fix-modifying-state-during-view-update-this-will-cause-undefined-behavior |
|
hi, i'd recommend that you not try to pop up an Alert any time there's an illegal input in a live edit/convert like this -- it will be pretty annoying, and it just won't happen that often if you have the decimal keypad on screen. (by the way, entering a negative temperature is a problem on the decimal pad.) instead, just replace the computation of the output converstion string so that it shows there's an error. just replace
the conversionResult could look something like this:
that should do it for you. a comment to consider: the logic of conversion could be simplified if you took a look at Foundation's support for Measurement. Paul has some information on this. hope that helps, DMG |
|
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.