ios - How do I convert a UITextField to an integer in Swift 2.0? -
i'm beginner @ swift, , doing testing. tell truth, barely know i'm doing yet, please try explain clearly. making random number generator app & wanted add in uitextfield
user can type in guess of next randomly generated number. keep getting error when trying use if statement compare randomly generated number number entered in text field.
import uikit class viewcontroller: uiviewcontroller, uitextfielddelegate { @iboutlet weak var input: uitextfield! @iboutlet weak var infolabel: uilabel! // displayed before user clicks button first time. @iboutlet weak var numberlabel: uilabel! @ibaction func go(sender: anyobject) { // remove text under button infolabel.text = " " // generate random number let randomnumber = int(arc4random_uniform(11)) // change "number label's" text in order show randomly generated number user numberlabel.text = "\(randomnumber)" // check see if user guessed correct number, , if did, tell them right if input == randomnumber { infolabel.text = "your guess,\(randomnumber) correct!" } } override func viewdidload() { super.viewdidload() } }
to convert input string integer using swift 2:
let guess:int? = int(input.text) if guess == randomnumber { // code here }
Comments
Post a Comment