ios - 如何将可映射对象保存到 nsuserdefault?
问题描述
我想将自定义整个对象保存到 NSUserDefault 吗?
这是我的自定义对象..
import UIKit
import ObjectMapper
class SAuthModel: Mappable {
var status: Int?
var message: String?
var result: SResultModel?
var authToken: String?
required init?(map: Map) {
}
func mapping(map: Map) {
status <- map["code"]
message <- map["message"]
authToken <- map["authToken"]
result <- map["data"]
}
}
class SResultModel: Mappable {
var name, email, countryCode, mobile, isNotification, promoCode, imageURL, _id, isMobileVerify,otp,faceBookId,googleId,stripeCustId,isProfileSet,created:String?
required init?(map: Map) {
}
func mapping(map: Map) {
name <- map["name"]
email <- map["email"]
countryCode <- map["countryCode"]
mobile <- map["mobile"]
isNotification <- map["isNotification"]
promoCode <- map["promoCode"]
imageURL <- map["imageURL"]
_id <- map["_id"]
isMobileVerify <- map["isMobileVerify"]
otp <- map["otp"]
faceBookId <- map["faceBookId"]
googleId <- map["googleId"]
stripeCustId <- map["stripeCustId"]
isProfileSet <- map["isProfileSet"]
}
}
SAuthService().logInService(parameters:userModel.toJSON()).done{(response) -> Void in
SVProgressHUD.dismiss()
if response.status == 210 {
guard let responseMessage = response.message else {
return
}
HHelper.showAlert(responseMessage)
} else if response.status == 200 {
USER_DEFAULTS.set(response.result, forKey:ConstantTexts.resultData.localizedString)
kAppDelegate.pushToHomeViewContoller()
}
}
我试图将自定义模型对象保存在 USER_DEFAULTS 中,但它一直在崩溃。
解决方案
本文描述了存储到 UserDefaults 的正确方法
var userDefaults = UserDefaults.standard
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: teams)
userDefaults.set(encodedData, forKey: "teams")
基本上你必须为它提供一个数据对象......
推荐阅读
- python - 如何恢复 PIP
- d3.js - c3.js - 如何手动触发甜甜圈弧的悬停?
- php - php表单提交和页面重定向
- haskell - 如何实现 fromJust 并遍历 Haskell 中的字符串列表
- reactjs - 元素已从文档中删除,但 jest 仍然找到它
- c - 如何将变量放入字符串 - C
- excel - 使用 VBA 隐藏列
- react-native - 如何在反应原生博览会的初始屏幕中为两个平台添加视频
- hyperledger-fabric - 在 Hyperledger Fabric 中,例如 channel1 上的 peer 如何访问 channel2 的账本?
- python - 如果前一个实例 dag 运行处于运行状态,则跳过当前 dag 运行