2
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)错误:无法使用类型为“(UInt32,@lvalue UInt32)”的参数列表调用“>”

4

1 回答 1

3

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

这一定是一个 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 没有在比较中进行转换类型,即使它应该这样做。有时它似乎搞砸了。使用带有集合类型的辅助变量可以解决这个问题。

于 2014-10-09T05:33:33.107 回答