随机数字语句不能与我的toInt文本字段输入一起使用

时间:2015-06-30 05:50:37

标签: ios swift

错误:不能使用类型'(@lvalue Int?,@ value Untnt32)的参数列表调用'=='

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

请告诉我为什么我在使用带有arcforrandom的==时遇到错误,以及我如何解决这个错误并让它像我想要的那样运行。非常感谢所有答案

3 个答案:

答案 0 :(得分:0)

Uint转换为Int

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

和..

为什么guess.text表示result.text

答案 1 :(得分:0)

UInt表示unsigned int,此处只允许使用正确的数字。

Int允许正面和负面

但两者都有相同的范围(2 ^ 31)

将您的UInt转换为Int

 if guess == Int(randonNumber) { 

答案 2 :(得分:0)

您的randomNumber属于UInt32类型。你必须将它转换为Int。 您可以在Apple Documentation

中阅读相关内容
var randomNumber = Int(arc4random_uniform(4)+1)