UserDefaults迅速4
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UserDefaults迅速4相关的知识,希望对你有一定的参考价值。
我有UserDefaults的问题。
import Foundation
class Utility: NSObject {
func storeBaseDetail(firstname:String,lastname:String,picStr:String,email:String,usrname:String,user_Id:String) {
UserDefaults.standard.set(firstname, forKey: "firstname")
UserDefaults.standard.set(lastname, forKey: "lastname")
UserDefaults.standard.set(picStr, forKey: "picStr")
UserDefaults.standard.set(email, forKey: "email")
UserDefaults.standard.set(usrname, forKey: "usrname")
UserDefaults.standard.set(user_Id, forKey: "id")
UserDefaults.standard.synchronize()
}
func getBaseDetail()->(firstName:String,lastName:String,picStr:String,email:String,usrname:String,user_Id:String){
return ((UserDefaults.standard.value(forKey: "firstname") as! String),(UserDefaults.standard.value(forKey: "lastname") as! String),(UserDefaults.standard.value(forKey: "picStr") as? String)!,(UserDefaults.standard.value(forKey: "email") as! String),(UserDefaults.standard.value(forKey: "usrname") as! String),(UserDefaults.standard.value(forKey: "id") as! String))
}
func storeBaseDetail(firstname:String,lastname:String,picStr:String,email:String,usrname:String,user_Id:String) {
UserDefaults.standard.set(firstname, forKey: "firstname")
UserDefaults.standard.set(lastname, forKey: "lastname")
UserDefaults.standard.set(picStr, forKey: "picStr")
UserDefaults.standard.set(email, forKey: "email")
UserDefaults.standard.set(usrname, forKey: "usrname")
UserDefaults.standard.set(user_Id, forKey: "id")
UserDefaults.standard.synchronize()
}
func getBaseDetail()->(firstName:String,lastName:String,picStr:String,email:String,usrname:String,user_Id:String){
return ((UserDefaults.standard.value(forKey: "firstname") as! String),(UserDefaults.standard.value(forKey: "lastname") as! String),(UserDefaults.standard.value(forKey: "picStr") as? String)!,(UserDefaults.standard.value(forKey: "email") as! String),(UserDefaults.standard.value(forKey: "usrname") as! String),(UserDefaults.standard.value(forKey: "id") as! String))
}
}
如果我称之为UserDefaults:
if Utility().getBaseDetail().firstName != ""{
userNameLbl.text = "(Utility().getBaseDetail().firstName) (Utility().getBaseDetail().lastName)"
}
我收到此错误:
线程1:致命错误:在解开Optional值时意外发现nil
答案
这是避免nil值的正确方法,
func getBaseDetail()->(firstName:String?,lastName:String?,picStr:String?,email:String?,usrname:String?,user_Id:String?){
return ((UserDefaults.standard.value(forKey: "firstname") as? String),(UserDefaults.standard.value(forKey: "lastname") as? String),(UserDefaults.standard.value(forKey: "picStr") as? String),(UserDefaults.standard.value(forKey: "email") as? String),(UserDefaults.standard.value(forKey: "usrname") as? String),(UserDefaults.standard.value(forKey: "id") as? String))
}
在其他类中创建Utility的实例,
let utilityInstance = Utility()
然后使用if let获取您的值
if let emailValue = utilityInstance.getBaseDetail().email{
print(emailValue)
}
所以,如果值在那里则会打印,如果没有值则会跳过。
另一答案
用这个改变你的功能
func getBaseDetail()->(firstName:String,lastName:String,picStr:String,email:String,usrname:String,user_Id:String){
return ((UserDefaults.standard.value(forKey: "firstname") as? String ?? ""),(UserDefaults.standard.value(forKey: "lastname") as? String ?? ""),(UserDefaults.standard.value(forKey: "picStr") as? String ?? ""),(UserDefaults.standard.value(forKey: "email") as? String ?? ""),(UserDefaults.standard.value(forKey: "usrname") as? String ?? ""),(UserDefaults.standard.value(forKey: "id") as? String ?? ""))
}
以上是关于UserDefaults迅速4的主要内容,如果未能解决你的问题,请参考以下文章