gpt4 book ai didi

ios - 将可解码结构存储到 Userdefaults

转载 作者:搜寻专家 更新时间:2023-11-01 06:30:25 26 4
gpt4 key购买 nike

我有一个像这样的 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。不确定如何实现...

最佳答案

添加 Codable 协议(protocol)的后半部分。为了与 init 方法兼容,date 属性根据 type

以不同方式保存
struct BestWish: Codable {

private enum CodingKeys : String, CodingKey {
case customerID = "customer_id"
case birthDate = "birth_date"
case anniversaryDate = "anniversary_date"
case type
case customerName = "customer_name"
case mobileNumber = "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: .mobileNumber)
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(customerID, forKey: .customerID)
try container.encode(type, forKey: .type)
if type == "anniversary_date" {
try container.encode(date, forKey: .anniversaryDate)
} else {
try container.encode(date, forKey: .birthDate)
}
try container.encode(customerName, forKey: .customerName)
try container.encode(mobileNumber, forKey: .mobileNumber)
}
}

然后对数组进行编码,将Data对象写入UserDefaults

do {
let jsonData = try JSONEncoder().encode(bestWishesArr)
UserDefaults.standard.set(jsonData, forKey:"bestWishes")
} catch { print(error) }

关于ios - 将可解码结构存储到 Userdefaults,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48207931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com