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

SOLVED: Question: Do I need to wrap up 'if' with a func or a closure ?

Forums > 100 Days of Swift

Or I could just use it straight out ?

In my code, Xcode red-warns me at the first line of if : expected declaration

import SwiftUI

struct ContentView: View {

    let threeMoves = ["Rock","Scisscors","Paper"]

    @State private var computerPick = Int.random(in: 0..<3)
    @State private var winOrLose = Bool.random()
    @State private var userScore = 0

    @State private var questionCount = 1
    @State private var showingResult = false
    @State private var correctAnswers = ""

            if threeMoves[computerPick] == "Rock" && winOrLose == true {
                correctAnswers ==  "Scissors"

            } else if threeMoves[computerPick]  ==  "Rock" && winOrLose == false {
                correctAnswers == "Paper"}

            else if threeMoves[computerPick]  ==  "Scissors" && winOrLose == true {
                correctAnswers ==  "Paper"}

            else if threeMoves[computerPick]  ==  "Scissors" && winOrLose == false {
                correctAnswers == "Rock"}

            else if threeMoves[computerPick]  ==  "Paper" && winOrLose == true {
                correctAnswers ==  "Rock"}

            else if threeMoves[computerPick]  ==  "Paper" && winOrLose == false {
                correctAnswers == "Scissors"}

            else {correctAnswers ==  "Not Applicable"}

1      

By the way, I got Paul's logic on this Challenge's solution, whic is creating another array of winning moves.

I just still want to make my own clumsy codes work. Try to drill wood to make fire,so to speak. I am using this Challenger to improve my understandings

Thank you in advance,

Boat

1      

Yes, you have to put statements, like if ... else ... inside a function or computed property. They can't be by themselves at the root level of a struct or class.

2      

Thank you @roosterboy

Have a great day ~

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out 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.