定义
private struct TokenModel: Codable {
var mobile: String
var token: String
var expired: Date
}
取
if let val = UserDefaults.standard.value(forkey: TOKEN_KEY) {
do {
var vardata = val as! Data
token = try JSONDecoder().decode(TokenModel.self, from: vardata)
} catch {
print("加载Token失败:Json无法解码成对象")
}
}
存
let tokenJson = try JSONEncoder().encode(self.token)
UserDefaults.standard.setValue(tokenJson, forkey: TOKEN_KEY)
网友评论