gpt4 book ai didi

swift - ( swift )错误 : can not invoke '>' with an argument list of type '(UInt32, @lvalue UInt32)'

转载 作者:可可西里 更新时间:2023-11-01 01:06:45 25 4
gpt4 key购买 nike

class ViewController: UIViewController {

@IBOutlet weak var inputField: UITextField!
@IBOutlet weak var output: UITextView!

var guesses : UInt = 0
var number : UInt32 = 0
var gameOver = false
let MAX_GUESSES : UInt = 8

@IBAction func guess(sender: UIButton) {
var possibleGuess : Int? = inputField.text.toInt()

if let guess = possibleGuess {
// possibleGuess exists!
} else {
consoleOut("Please input a valid number!\n")
clearInput()
}

if UInt32(guess) > Int(number) {
consoleOut("\(guess): You guessed too high!\n")
++guesses
} else if UInt32(guess) < number {
consoleOut("\(guess): You guessed too low!\n")
++guesses
} else {
consoleOut("\n\(guess): You win!\n")
consoleOut("Go again? (Y)")
guesses = 0
gameOver = true
}
clearInput()

if (guesses == MAX_GUESSES) {
consoleOut("\nYou lose :(\n")
consoleOut("Go again? (Y)")
guesses = 0
gameOver = true
}

}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

number = generateNewNumber()
consoleOut("Gondolkodom egy számot...\n")
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func consoleOut(text : String) {
output.text = output.text + text
}


func generateNewNumber () -> UInt32 {
return arc4random_uniform(100)
}

func clearInput() {
inputField.text = ""
}

}

这是我使用的代码,我在 if UInt32(guess) > Int(number) { 处收到错误消息。我真的受不了了。

(swift) Error: can not invoke '>' with an argument list of type '(UInt32, @lvalue UInt32)'

最佳答案

* 这不完全是你的问题,但它可能会告诉你一个解决它的方法:) *

这一定是一个 Swift 错误,就像 ObjectiveC 的许多其他错误一样。我在尝试比较 arc4random() 数字(它是 UInt32 类型)与 UInt32() 转换字符串时遇到了同样的问题,我得到了同样的错误,这在我的例子中更离谱,因为这两个数字是同一类型。这让我认为类型转换一定没有产生预期的结果。

我虽然创建了一个辅助 UIint32 变量并为其赋值 UInt32(theString),但是 Swift 不允许您在定义变量时将 String 转换为 UInt32,因此我不得不创建一个辅助变量以转换为 Int,并且然后将 Int 转换为 UInt32 以便能够比较两个数字:

var theString = "5"
var randomNumber = arc4random() % 10

var UInt32Number = UInt32(theString)
// => ERROR: "Cannot invoke 'init' with an argument of type '@lvalue String!'
// (this is where I realized the comparison line could be suffering from the same problem)
if randomNumber == UInt32(theString) { ... }
// No error here 'cos Swift is supposed to have casted theString into a UInt32
// ...but surprisingly it prompts an ERROR saying it can't compare a UInt32 with a UInt32 (WTF!)
// And here's where I go crazy, because watch what happens in the next lines:

var intValue = theString.toInt()
var UInt32Value = UInt32(intValue!)
if randomNumber == UInt32Value { ... } // => NOW IT WORKS!!

结论:Swift 没有在比较中创建转换类型,即使它应该这样做。有时它看起来很糟糕。使用具有集合类型的辅助变量可以解决这个问题。

关于swift - ( swift )错误 : can not invoke '>' with an argument list of type '(UInt32, @lvalue UInt32)' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25898004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com