错误:不能使用类型'(@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的==时遇到错误,以及我如何解决这个错误并让它像我想要的那样运行。非常感谢所有答案
答案 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)