将可解码结构存储到Userdefaults

时间:2018-01-11 13:10:35

标签: ios swift

我有一个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。不知道如何实现......

1 个答案:

答案 0 :(得分:3)

添加Codable协议的后半部分。要与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) }