ios - How to create an if statement with an array in swift? -
i trying make app uses login system. have created array both usernames , passwords. need test if password correct or not. how can create if statement tests values inside of array? here code in case need it.
import uikit var namesarray = [string]() var passwordarray = [string]() class viewcontroller: uiviewcontroller { var username = "ege" var password = "gürkan" var truefalse = true @iboutlet weak var idtextfield: uitextfield! @iboutlet weak var passwordtextfield: uitextfield! @iboutlet weak var truewronglabel: uilabel! @ibaction func buttonpressed(sender: anyobject) { if idtextfield.text == "ege" && passwordtextfield.text == "gürkan" { self.performseguewithidentifier("geçiş", sender: self) } else { truewronglabel.text = "wrong id/pw" idtextfield.resignfirstresponder() passwordtextfield.resignfirstresponder() truewronglabel.textcolor = uicolor.redcolor() idtextfield.text = nil passwordtextfield.text = nil } } override func viewdidload() { super.viewdidload() nsuserdefaults.standarduserdefaults().setobject(namesarray, forkey: "namearray") var recallednamearray = nsuserdefaults.standarduserdefaults().objectforkey("namearray")! nsarray nsuserdefaults.standarduserdefaults().setobject(passwordarray, forkey: "passwordarray") var recalledpasswordarray: anyobject? = nsuserdefaults.standarduserdefaults().objectforkey("passwordarray") } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } } in line says: if istextfield.text == "ege" want test if there value has user entered in textfield. how can it?
ps: new programming in general please don't use complicated terms while explaining
this beginning programming stuff.
there lots of ways this.
one straightforward way create struct contains name property , password property. create array of structs. when user enters username , password, use filter command find structure contains matching username, , check see if password correct.
if you're not comfortable swift filter function (i haven't used yet myself) loop through array looking matches.
Comments
Post a Comment