简体   繁体   中英

Store Decodable struct to Userdefaults

I have a Decodable struct like so...

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)
    }
}

And all the data within this is stored in an array like so..

var bestWishesArr = [BestWishes]()
self.bestWishesArr.append(contentsOf: result.bestWishes) 

Now I would like to store bestWishesArr to Userdefaults. Not sure how to achieve that...

Add the second half of the Codable protocol. To be compatible with the init method the date property is saved differently depending on 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)
    }
}

Then encode the array and write the Data object to UserDefaults

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM