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

Day 34 - A simple problem

Forums > 100 Days of SwiftUI

Hello everyone!

I messed up the code from the flag project too much, so I created a new application from scratch.

What do I have to do to make it work? When my code looks like this: "@State private var playerChoose = "" "

ForEach(0..<3) { number in
        Button {
          playerChoose == emoji[number] // RESULT OF OPERATOR '==' IS UNUSED

If I change "@State private var playerChoose = Int()" -> I get this error: Binary operator '==' cannot be applied to operands of type 'Int' and 'String'

So here is my code.

import SwiftUI

struct EmojiView: View {
    @State private var emoji = ["๐Ÿ", "๐ŸŽ", "๐Ÿ", "๐ŸŠ", "๐Ÿ‹"]
    @State private var random = Int.random(in: 0...2)

    @State private var playerChoose = ""
    @State private var spinArround = 0.0
    @State private var opacity = 1.0

    var body: some View {
        VStack {
            Text("ForEachLoop.V3")
                .font(.title2)
                .fontWeight(.medium)
            Divider()

            HStack {
                ForEach(0..<3) { number in
                    Button {
                        playerChoose == emoji[number]  // RESULT OF OPERATOR '==' IS UNUSED
                        opacity -= 0.75
                        spinArround += 360

                    } label: {
                        playerChoose == emoji[number]  ?
                        Text(emoji[number])
                            .opacity(1)
                            .rotation3DEffect(.degrees(spinArround), axis: (x: 0, y: 1, z: 0))

                        :
                        Text(emoji[number])
                            .opacity(opacity)
                            .rotation3DEffect(.degrees(0), axis: (x: 0, y: 1, z: 0))
                    }
                }

   

You should use " playerChoose = emoji[number] ", the " = " ( not the " == " ) is the right way to run the code. And there is no animation in your code, so you should add " withAnimation { } " into the button action where you set the value of opacity and spinArround.

Button {
    playerChoose = emoji[number]
    withAnimation {
        opacity -= 0.75
        spinArround += 360
    }                        
}

   

@Giant is correct.

// == means Are these two elements the same?
if playersSelection == emoji[number] {  // <-- Are these the same?
     print("They are the same!")
}

// = means Assign this to that.
playersSelection = emoji[number]  // <-- Players selection gets a new value

   

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.