using xcode 7 beta, swift 2.0
i'm saving , loading credentials keychain, somehow when loading "optional(value)" back, looks part of string displayed in textbox or when sending api
this how save , load credentials now, see i've done lot of nil checking make sure not nil or optional, indeed overuse of explanation marks...
func savecredentials(credentials : [string : string!]!) -> bool { if(credentials.count == 2) { //only proceed when have 2 keys: username , password let username = credentials["username"] let password = credentials["password"] if let usernamestr = username {//also tried username!=nil && password != nil if let passwordstr = password { //usernamestr , passwordstr of type string! let nsdatausername = usernamestr!.datausingencoding(nsutf8stringencoding) let nsdatapassword = passwordstr!.datausingencoding(nsutf8stringencoding) if(nsdatausername != nil && nsdatapassword != nil) { localstorage.savetokeychain("username", data: nsdatausername!) localstorage.savetokeychain("password", data: nsdatapassword!) return true } } } } return false } func loadcredentials() -> [string : string!]? { let nsdatausername = localstorage.loadfromkeychain("username") let nsdatapassword = localstorage.loadfromkeychain("password") if(nsdatausername != nil && nsdatapassword != nil) { let username : string! = string(nsstring(data: nsdatausername!, encoding: nsutf8stringencoding)) let password : string! = string(nsstring(data: nsdatapassword!, encoding: nsutf8stringencoding)) if let usernamestr = username { if let passwordstr = password { // password of type string!, passwordstr of type string var credentials : [string: string!] = [string : string]() credentials["username"] = usernamestr credentials["password"] = passwordstr return credentials } } } return nil } and when send api, method requires non-optional string. method work when logging in, getting strings text fields, not filter out optional when coming keychain.
func loginuser(email : string!, password : string!) { print("loginuser(email : \(email), password: \(password))") var parameters = [string : anyobject]() parameters["username"] = email parameters["password"] = password ...... the strings send savecredentials method, same user logged in with:
func loginlocalaccount(email : string!, password : string!) { databaseapi.loginuser(email!, password: password!) //login goes fine savecredentials(email!, password: password!) //manages optional in it.. } i suspect has saving , loading keychain, interests, this use save , load keychain.
i want rid of them because when app starts, loads credentials , tries login @ api. ofcourse error username not valid e-mail, because optional(email@adress.com)
you're overusing !. don't need them. try learn more implicitly unwrapped optionals, optionals, ... code mess (no offense, everybody's learning).
back optional problem, it's caused line:
let username : string! = string(nsstring(data: nsdatausername!, encoding: nsutf8stringencoding)) convenience init?(data: nsdata, encoding: uint) - inner part utilizes failable initializer, so, nsstring? result. initialization of string optional nsstring? produces optional well. but, has no sense @ in way.
first part - remove optional
utilizing new guard:
guard let loadedpassword = nsstring(data: passworddata, encoding: nsutf8stringencoding) else { fatalerror("ooops") } loadedpassword contains nsstring (not nsstring?) now.
second part - nsstring -> string
you did read (if not, read) strings , characters bridging, ... if can freely exchange nsstring string, can think you're done:
var dict = [string:string]() dict["password"] = loadedpassword nope. produces following error:
nsstringnot implicitly convertiblestring; did mean use 'as' explicitly convert?
slight change , you're done:
var dict = [string:string]() dict["password"] = loadedpassword string complete example
let password = "hallo" guard let passworddata = password.datausingencoding(nsutf8stringencoding) else { fatalerror("ooops") } // save/load to/from keychain guard let loadedpassword = nsstring(data: passworddata, encoding: nsutf8stringencoding) else { fatalerror("ooops") } var dict = [string:string]() dict["password"] = loadedpassword string print(dict) // "[password: hallo]\n"
Comments
Post a Comment