Ask coding questions

← Back to all posts
error cannot assign value of type int to string swift - what am I doing wrong?
joshuam6060 (0)

print("guess a number that I thought of")
let number = Int.random(in: 0...100)
var z = readLine()
if z = number{
print("yay")
}

Answered by Coder100 (8720) [earned 5 cycles]
View Answer
Commentshotnewtop
Coder100 (8720)

Right here, you forgot an =. Remember. = is for assignment and == is for equality:

if z == number {
joshuam6060 (0)

@Coder100
I tried that and it didn't work.

Coder100 (8720)

z has to be of type string @joshuam6060

Coder100 (8720)

oh right, z can be optional.

if Int(z ?? "0") == number {

@joshuam6060