UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 25 - function to text Output in View not working.

Forums > 100 Days of SwiftUI

@Anas  

Hi there! I'm currently learning Swift and I've encountered an issue where the function I've created is supposed to give a text output based on a random number - however, there's no output on the preview - just a blank screen. Same thing with Bool.random() too. This may not be the most effecient method to get the desired result, but if there's an explanation on why this particular way isn't working that'd be much appreciated. Thanks!

struct ContentView: View {

@State var shouldWin = Int.random(in: 0...2)
@State var alertMessage = ""
@State var number = Int.random(in: 0...2)

func WinOrLose() -> String {
    if number == shouldWin {
        alertMessage = "You need to win this round!"
    }
    else {
        alertMessage = "You need to lose this round"
    }

    return alertMessage
}

var body: some View {

    Text("\(alertMessage)")

}
   }

2      

Text is a view, and first, you never call winOrLose(), so nothing ever happens to alertMessage and it remains at its initialised value of "". (Note - convention is that functions always start with a lowercase letter - so winOrLose() not WinOrLose())

The easiest way around this is to add the .onAppear function call extension to Text.

Second, as you already set the value of alertMessage, which is a @State variable, then there is no need for the return statement. In fact this would cause a problem when calling the .perform: action in the .onAppear.

I have also given a couple of examples (commented out) of a ternary version of the if statement, that could be used here as an alternative method to setting alertMessage.

struct ContentView: View {
    @State var shouldWin = Int.random(in: 0...2)
    @State var alertMessage = ""
    @State var number = Int.random(in: 0...2)

    func winOrLose() {
        if number == shouldWin {
            alertMessage = "You need to win this round!"
        }
        else {
            alertMessage = "You need to lose this round"
        }

//        alertMessage = (number == shouldWin ?
//            "You need to win this round!" :
//            "You need to lose this round")

//        alertMessage = "You need to \(number == shouldWin ? "win" : "lose") this round!"
    }

    var body: some View {

        Text(alertMessage)
            .onAppear(perform: winOrLose)
    }
}

4      

@Anas  

Thank you very much @Greenamberred for the quick and detailed answer! I've tried implementing each of your solutions, so it was great to have two ways to try achieve this! Was a head scratcher for some time haha, thanks again!

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.