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

SOLVED: Day 25 - Milestone -> Loops

Forums > 100 Days of SwiftUI

Hi guys,

In our milestone project - "ScissorsPaperRock", Pual was using " @State private var computerChoice = Int.random(in: 0..<3)" for the emoji choices [ 🤚,✊,✌️]. But if I tried to say : "Int.random(in: 0...2) ", Xcode refused to build my code. As my understanding, 0..<3 is same meaning as 0...2, they all loop from Paper through Scissors.

For example, let myArray = ["Tom", "Peter", "Allen"], where Tom is [0], Peter is [1] and Allen is [2]. Then Int.random(in: 0..<3) loops [0],[1],[2], and Int.random(in: 0...2) also loops [0],[1],[2].

But in our project 2 -" Guess the Flags". Pual was using " @State private var correctAnswer = Int.random(in: 0...2)", and that works.

So when should I use (0..<3) , and when should I use (0...2) ?

Thank you guys.

Leona

2      

Hi Leona,

I have changed @State private var computerChoice = Int.random(in: 0...2) in the RockPaperScissors Project. And it worked the same with the Int.random(in: ), as this except a Range (..<) or Closed Range (...), however there are some function that require only a Range.

Nigel

2      

Thanks Nigel

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

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.