|
My project will build rine, but none of my alerts are apearing and I have no clue why. If anyone could help me figrue out what my error is that would be awsome. Project: RockPaperScissors Project |
|
A quick look at your code I see the problem is that you've got two alerts, one added to a ZStack, and then another one added to the first alert. That's not going to work for you as it seems like you can only add one alert to a view. https://stackoverflow.com/a/59116329 What you could do is have a more generic alert that has its content customized based on the reason you're showing it. So instead of two alerts:
Use a single generic alert that can show whatever you need to, as well as call whatever method you need when showing the alert. An important thing to remember is that you can store references to methods to pass them around and use in different places with different names.
Add a method that handles all of the reset game responsibilities:
And change up the clickButton method:
And then make sure you have more state variables to use for the generic alert:
|
|
The entire ContentView file that worked for me is below:
Happy hacking! |
|
*I am not an expert. I'm a beginner like you, so take EVERYTHING I say, knowing I could be completely off key. I'm a curious beginner like you. However, I took a look, and you have both alerts attached to the same stack. SwiftUI can only handle one alert to each item. Since you attached two to the same, the second alert will overwrite the first and SwiftUI will never check for the first. I found this out by changing your initial 'show' values to true, one at a time, which will open the alert when starting the app. Changing the winLoseAlertShowing = true didn't do anything, but changing the resetGameAlertShowing = true showed the alert when opening the app. Then, if you delete the reset game alert, then it'll show the first alert because it's no longer overridden. I got it to work with both by attaching one alert to the Button() and one to a Text(). A good way to test it would be set one 'show' value to true and the other to false, then switch them checking both times to make sure the correct alerts shows. Paul explains it better here: https://www.hackingwithswift.com/quick-start/swiftui/how-to-show-multiple-alerts-in-a-single-view I hope this helps. |
|
@chadchabot I cant find the algorythm in your code the challenge says:
Which as I understand means you are given choice to win or loose i.e. if random picks scissors and asks you to loose then you have to choose paper to win. So here is what I came up with.
|
SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.
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.