TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Hacking with MacOS: SwiftUI Edition

Forums > Books

I'm currrently working through the book, Hacking with MacOS: SwiftUI Edition, and on project 3 there is: “For a bigger challenge, go back to project 2 and make the result text show in different colors depending on how close the user is: red for 0 cows 0 bulls, yellow for anything between 1 and 3 cows, and green for 4 cows"

You can look at the code for project 2 here:https://github.com/twostraws/macOS/tree/main/SwiftUI/project2/CowsAndBulls

I was wondering if anyone could help point me in the right direction with how to go about this challenge?

3      

Hey there :) I hope it is not to late... I am completly new to Swift UI and Mac OS in generall, so it is, perhaps, not the best solution. Here is what i am done. I'm using a View for that:

struct ColoredText: View {
  let text: String
  let result: String
  private var color: Color {
    if result.contains("0🐄") {
      return .red
    } else if result.contains("4🐄") {
      return .green
    }
    return .yellow
  }

  var body: some View {
    Text(text)
      .foregroundStyle(color)
  }
}

placing it over the ContentView in the same file... (and i use emoji's insted of "c" for the cow)

and in "ContentView.swift" Line 35 (github example) replace

Text(guess)

with

ColoredText(text: guess, result: result(for: guess))

   

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.