2014-09-17 1 views
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)'형식의 인수 목록 '>'호출 할 수는

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

+0

더 많은 코드를 표시 하시겠습니까? 적어도 컴파일하고 실행할 수 있어야합니다. – Kaydell

답변

3

*이 정확히 문제가 아니라 그것은 당신에게 그것의 주위에 얻을 수있는 방법 : * 표시 될 수 있습니다

이 오브젝티브 -c가 가진 많은 다른 사람과 같은 스위프트 버그해야합니다. 나는 동일한 문제를 UInt32() casted 문자열로 arc4random() 숫자 (UInt32 유형)와 비교하려고 시도하고 있는데, 동일한 오류가 발생합니다. 두 개의 숫자가 ARE 같은 유형. 그래서 나는 주물이 원하는 결과를 만들어 내지 않아야한다고 생각하게 만든다.

보조 UIint32 변수를 만들고 UInt32 (theString)을 할당했지만 변수를 정의 할 때 문자열을 UInt32로 변환 할 수 없기 때문에 보조 변수를 만들어 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!! 

결론 : 스위프트가 돼있 경우에도 비교하여 변환 유형을하지 않습니다. 때때로 그것은 f *** 위로 보인다. 집합 타입에 보조 변수를 사용하면 문제를 해결할 수 있습니다.

관련 문제