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

Day 25: Challange - A simple problem?

Forums > 100 Days of SwiftUI

Hi everyone!

I will probably look like a fool now. But I can't find a simple solution.

Here is my code:

struct ContentView: View {

  @State private var figures = ["🪨", "✂️", "📄"] //
  @State private var figuresWin = ["✂️", "📄", "🪨"]
  @State private var figuresLose = ["📄", "🪨", "✂️"] // 
  @State private var random = Int.random(in: 0...2)

 @State private var winOrLose = Bool()

   // HERE IS MY FIRST TRY 
    var winOrNot: Bool {
        if winOrLose == true {
            print("Win", font(.subheadline.bold()), foregroundColor(.yellow))
        } else {
            print("Lose", font(.subheadline.bold()), foregroundColor(.red))
        }
        return winOrLose
    }

    // HERE IS MY SECOND TRY 
    var winGame: String {
        Text("Win")
            .font(.subheadline.bold())
            .foregroundColor(.yellow) // Cannot convert return expression of type 'Text' to return type 'String'
    }

    var loseGame {  
            Text("Lose")
            .font(.subheadline.bold())
            .foregroundColor(.red)
    }

    var currentPick: String {
        figures[random]
    }

    var body: some View {
        VStack {
            Text("Hello, world!")
            HStack {
                Text("Challenge!")
                    .font(.subheadline.bold())
                Text(winOrLose) // FIRST WAY - Not work ( No exact matches in call to initializer ) 

                Text("\(winOrLose ? winGame : loseGame)") // SECOND WAY - Not work 

                Text("\(winOrLose ? "Win" : "Lose")") // THIRD WAY - It works 
            }

My question is:

  1. How can I make the second and third way work? I would like to create text that would change color depending on what it displays. In my case, I want the "win" to be yellow and the "lose" to be red

2      

First you need to remember that Text is a View, and not a sequence of characters (which is a String). Text uses a String and Text modifiers to create the View displayed.

This is why your first and second cases do not work. In the first case you have parameter which is a Bool, and in the second case your parameters are Text.

Try this above your ContentView

extension Text {          // defining a new text style as an extension to Text

    func winOrLoseStyle (_ status: Bool) -> some View {
        self.foregroundColor(status == true ? Color.red : Color.yellow)
            .font(.subheadline.bold())
    }
}

then modify your third way to be

Text("\(winOrLose ? "Win" : "Lose")") // THIRD WAY - It works
   .winOrLoseStyle(winOrLose)

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.