|
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 |
|
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.