//this is my model
struct RestaurantItem : Codable {
var v : Int?
var id : String = ""
let addOnList : [AddOnList]?
var createdAt : String = ""
var orderType : Int?
var productId : ProductId?
var quantity : Int?
var restaurantId : String = ""
var specialInstructions : String = ""
var updatedAt : String = ""
var userId : String = ""
enum CodingKeys: String, CodingKey {
case v = "__v"
case id = "_id"
case addOnList = "addOnList"
case createdAt = "createdAt"
case orderType = "orderType"
case productId = "productId"
case quantity = "quantity"
case restaurantId = "restaurantId"
case specialInstructions = "specialInstructions"
case updatedAt = "updatedAt"
case userId = "userId"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
v = try values.decodeIfPresent(Int.self, forKey: .v)
id = try values.decodeIfPresent(String.self, forKey: .id) ?? ""
if (try? values.decodeIfPresent(String.self, forKey: .addOnList)) == nil {
self.addOnList = try values.decodeIfPresent([AddOnList].self, forKey: .addOnList)
} else {
self.addOnList = nil
}
createdAt = try values.decodeIfPresent(String.self, forKey: .createdAt) ?? ""
orderType = try values.decodeIfPresent(Int.self, forKey: .orderType)
quantity = try values.decodeIfPresent(Int.self, forKey: .quantity)
restaurantId = try values.decodeIfPresent(String.self, forKey: .restaurantId) ?? ""
specialInstructions = try values.decodeIfPresent(String.self, forKey: .specialInstructions) ?? ""
updatedAt = try values.decodeIfPresent(String.self, forKey: .updatedAt) ?? ""
userId = try values.decodeIfPresent(String.self, forKey: .userId) ?? ""
if (try? values.decodeIfPresent(String.self, forKey: .productId)) == nil {
self.productId = try values.decodeIfPresent(ProductId.self, forKey: .productId)
} else {
self.productId = nil
}
}
}
Decalare in viewcontroller
var restaurantItems : [RestaurantItem]?
// this array contain dublicate value based on "id
", How can i get a new array without replcate data