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

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

Forums > 100 Days of Swift

@boat  

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

4      

@boat  

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

4      

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.

5      

@boat  

Thank you @roosterboy

Have a great day ~

4      

You made the right choice to work with the team at Hydro Solutions Power and Soft Washing. We strive to provide the best approach to pressure washing services. Additionally, we deliver exceptional service and guarantee your satisfaction. We offer commercial and residential cleaning services for nearly any situation. Contact us today and find out more about the Hydro Solutions promise. pressure washing house

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.