【发布时间】:2018-01-11 13:10:35
【问题描述】:
我有一个像这样的Decodable 结构......
struct BestWishes: Decodable {
private enum CodingKeys : String, CodingKey {
case customerID = "customer_id"
case birthDate = "birth_date"
case type = "type"
case customerName = "customer_name"
case mobileNo = "mobile_number"
}
let customerID: Int
let date: String
let type: String
let customerName : String
let mobileNumber: Int
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
customerID = try container.decode(Int.self, forKey: .customerID)
type = try container.decode(String.self, forKey: .type)
if type == "anniversary_date" {
date = try container.decode(String.self, forKey: .anniversaryDate)
} else {
date = try container.decode(String.self, forKey: .birthDate)
}
customerName = try container.decode(String.self, forKey: .customerName)
mobileNumber = try container.decode(Int.self, forKey: .mobileNo)
}
}
并且其中的所有数据都存储在这样的数组中..
var bestWishesArr = [BestWishes]()
self.bestWishesArr.append(contentsOf: result.bestWishes)
现在我想将bestWishesArr 存储到 Userdefaults。不知道如何实现...
【问题讨论】:
-
使用keyarchiver。请参考以下stackoverflow.com/a/37983027/1142743
-
不,@Vinodh 它似乎不起作用。我的结构是 Decodable 类型的
-
有可能然后你接受了答案。在不知道/尝试任何事情的情况下,不要说这是不可能的