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

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))
                    }
                }

2      

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
    }                        
}

2      

@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

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.