Jacob Cafiero
Jacob Cafiero

Reputation: 11

random number statement is not working with my toInt text field input

error bellow: Cannot invoke '==' with an argument list of type '(@lvalue Int?, @lvalue UInt32)

@IBOutlet weak var textBox: UITextField!

@IBOutlet weak var result: UILabel!

@IBAction func submit(sender: AnyObject) {
    var randomNumber = arc4random_uniform(4)+1//look at the first if declaration. This is were my question is
    var guess = textBox.text.toInt()
    if guess == randomNumber {             //error: Cannot invoke '==' with an argument list of type '(@lvalue Int?, @lvalue UInt32)
        guess.text = "Well done, correct!"
    }
    else if guess < 0 {
        guess.text = "WHAT!?!? that wasn't even an option"
    }
    else if guess > 5 {
        guess.text = "WHAT!?!? that wasn't even an option"
    }
    else {
        guess.text = "WRONG the correct answer was \(randomNumber)"
    }

Please tell me why I'm getting an error using an == with the arcforrandom and how i can fix this and have it run like i want it to. All answers are much appreciated

Upvotes: 1

Views: 67

Answers (3)

Raja Vikram
Raja Vikram

Reputation: 1980

UInt means unsigned int only possitive numbers are allowed here.

Int allows both positive and negative

But both have same range (2^31)

So typecast your UInt to Int

 if guess == Int(randonNumber) { 

Upvotes: 0

heikomania
heikomania

Reputation: 455

Your randomNumber is of the type UInt32. You have to convert it to an Int. You can read about it in the Apple Documentation

var randomNumber = Int(arc4random_uniform(4)+1)

Upvotes: 0

Reming Hsu
Reming Hsu

Reputation: 2225

convert Uint to Int

var randomNumber = Int( arc4random_uniform(4)+1 )

and..

why guess.text you mean result.text?

Upvotes: 0

Related Questions