(swift)错误:无法调用'>'使用类型'(UInt32,@lvalue UInt32)'的参数列表

时间:2014-09-17 18:39:25

标签: swift uint32

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)错误:无法调用'&gt;'使用类型'(UInt32,@ lvalue UInt32)'

的参数列表

1 个答案:

答案 0 :(得分:3)

*这不是你的问题,但它可能会告诉你如何解决它:) *

这必须是像ObjectiveC那样的许多其他人的Swift错误。 我有同样的问题试图比较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没有在比较中制作转换类型,即使它应该是。有时似乎f *** up。使用具有集合类型的辅助变量可以解决问题。