WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

   

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)

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, 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.

Save 50% on all our books and bundles!

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

Reply to this topic…

You need to create an account or log in to reply.

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.