ios - Flow Control in Xcode -


for reason, if input valid input, namely, integer, seem go if statement in function, @ibaction func guess(sender: uibutton){}. in other words, if input 5, console output "please input valid number." not "you guessed high" or "you guessed low" or "you win!". suggestion on how fix this?

the following code:

class viewcontroller: uiviewcontroller {      //just member variables. don't think problem here.      @iboutlet weak var inputfield: uitextfield!     @iboutlet weak var output: uitextview!     var guesses : uint = 0;     var number : uint32 = 0;     var gameover = false;     let max_guesses = 8;     var possibleguess : int?      override func viewdidload() {         super.viewdidload()         number = generatenewnumber()         consoleout("i'm thinking of number...\n")         var possibleguess : int? = inputfield.text.toint()     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }     //consoleout here prints out stuff on app.      func consoleout(text : string) {         output.text = output.text + text;     }      func generatenewnumber() -> uint32 {         return arc4random_uniform(100) + 1     }      func clearinput() {         output.text = ""     }      //here lies problem. control never seems go inside of     //if let guess = possibleguess{}     @ibaction func guess(sender: uibutton) {         if let guess = possibleguess {             if uint32(guess) > number {                 consoleout("\(guess): guessed high\n")                 ++guesses             }             else if uint32(guess) < number {                 consoleout("\(guess): guessed low\n")                 ++guesses             }             else {                 consoleout("\(guess): win!\n")                 consoleout("go again? (y/n)")                 guesses = 0                 gameover = true             }             clearinput()         }         else {             clearinput()             consoleout("please input valid number")         }     } } 

this common mistake.

you declare variable possibleguess optional int instance variable, later declare variable same name in viewdidload() scope locally within viewdidload() method.

solution : delete var keyword in viewdidload() method.

possibleguess = inputfield.text.toint() 

the explicit type annotation not needed type has been defined.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -