我的 Userdefault 没有保存数据,它没有被 ObjectMapper 在 swift 中映射
My Userdefault is not saving data and its not being mapped by ObjectMapper in swift
我希望客户注册并在输入数据后将其映射到用户默认值并保存在那里,当我想验证用户时我调用一个函数验证从用户那里获取保存的数据验证用户,但 GET 中的数据映射不起作用
var UserInfoDefualts: UserModel {
set {
let userJson = try! JSONEncoder().encode(newValue)
let jsonString = String(data: userJson, encoding: .utf8)!
KeychainWrapper.standard.set(jsonString, forKey: "UserData")
}
get {
let userJosn = KeychainWrapper.standard.string(forKey: "UserData") ?? ""
let userData = Mapper<UserModel>().map(JSONString: userJosn) ?? UserModel()
return userData
}
}
get 未映射数据 userdata 始终为 NIL,在其中正确设置其 Filled
注册功能
@IBAction func continueButtonClicked(_ sender: UIButton) {
ApiServices.shared.signUp(name: fullName.text!, number: phoneNumber.text!, gender: 1, date: birthDate.text!) { (result, statusCode) in
print(result)
if statusCode == 200 {
let userData = result
print(userData.fullName)
UserInfoDefualts = userData
ScreenLoader().pushVC(rootView: self, viewController: Verify_VC)
// Shared.logIn(userData: userData)
}
}
}
校验函数
@IBAction func verifyClicked(_ sender: Any) {
var data = UserInfoDefualts
ApiServices.shared.verifyUser(userID: UserInfoDefualts.userId ?? 0, otpCode: self.code) { (result, statusCode) in
if statusCode == 200 {
ScreenLoader().pushVC(rootView: self, viewController: Welcome_VC)
}
}
}
在结构中将 Encodable
替换为 Codable
并删除与 ObjectMapper
相关的所有代码
然后将计算出的属性替换为
var userInfoDefaults: UserModel? {
set {
let userJson = try! JSONEncoder().encode(newValue!)
let jsonString = String(data: userJson, encoding: .utf8)!
KeychainWrapper.standard.set(jsonString, forKey: "UserData")
}
get {
guard let userJosn = KeychainWrapper.standard.string(forKey: "UserData") else { return nil }
let data = Data(userJosn.utf8)
do {
return try JSONDecoder().decode(UserModel.self, from: data)
} catch {
print(error)
return nil
}
}
}
由于 UserModel
没有自定义初始值设定项,因此计算出的 属性 必须是可选的。所以你必须写在 IBAction
guard let data = userInfoDefaults else { return }
您可以像下面这样在 userdefault 中存储 objectmapper 对象:
if let userInfo = Mapper<SignInModel>().map(JSONObject: dictionary["signup"]) {
if let JSONString = Mapper().toJSONString(userInfo, prettyPrint: true) {
//User Info:-
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: JSONString)
UserDefaults.standard.set(encodedData, forKey: "userinfo")
}
我希望客户注册并在输入数据后将其映射到用户默认值并保存在那里,当我想验证用户时我调用一个函数验证从用户那里获取保存的数据验证用户,但 GET 中的数据映射不起作用
var UserInfoDefualts: UserModel {
set {
let userJson = try! JSONEncoder().encode(newValue)
let jsonString = String(data: userJson, encoding: .utf8)!
KeychainWrapper.standard.set(jsonString, forKey: "UserData")
}
get {
let userJosn = KeychainWrapper.standard.string(forKey: "UserData") ?? ""
let userData = Mapper<UserModel>().map(JSONString: userJosn) ?? UserModel()
return userData
}
}
get 未映射数据 userdata 始终为 NIL,在其中正确设置其 Filled
注册功能
@IBAction func continueButtonClicked(_ sender: UIButton) {
ApiServices.shared.signUp(name: fullName.text!, number: phoneNumber.text!, gender: 1, date: birthDate.text!) { (result, statusCode) in
print(result)
if statusCode == 200 {
let userData = result
print(userData.fullName)
UserInfoDefualts = userData
ScreenLoader().pushVC(rootView: self, viewController: Verify_VC)
// Shared.logIn(userData: userData)
}
}
}
校验函数
@IBAction func verifyClicked(_ sender: Any) {
var data = UserInfoDefualts
ApiServices.shared.verifyUser(userID: UserInfoDefualts.userId ?? 0, otpCode: self.code) { (result, statusCode) in
if statusCode == 200 {
ScreenLoader().pushVC(rootView: self, viewController: Welcome_VC)
}
}
}
在结构中将 Encodable
替换为 Codable
并删除与 ObjectMapper
然后将计算出的属性替换为
var userInfoDefaults: UserModel? {
set {
let userJson = try! JSONEncoder().encode(newValue!)
let jsonString = String(data: userJson, encoding: .utf8)!
KeychainWrapper.standard.set(jsonString, forKey: "UserData")
}
get {
guard let userJosn = KeychainWrapper.standard.string(forKey: "UserData") else { return nil }
let data = Data(userJosn.utf8)
do {
return try JSONDecoder().decode(UserModel.self, from: data)
} catch {
print(error)
return nil
}
}
}
由于 UserModel
没有自定义初始值设定项,因此计算出的 属性 必须是可选的。所以你必须写在 IBAction
guard let data = userInfoDefaults else { return }
您可以像下面这样在 userdefault 中存储 objectmapper 对象:
if let userInfo = Mapper<SignInModel>().map(JSONObject: dictionary["signup"]) {
if let JSONString = Mapper().toJSONString(userInfo, prettyPrint: true) {
//User Info:-
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: JSONString)
UserDefaults.standard.set(encodedData, forKey: "userinfo")
}