gpt4 book ai didi

swift - 按该值中的特定对象过滤掉对象

转载 作者:行者123 更新时间:2023-11-30 10:46:53 27 4
gpt4 key购买 nike

所以我有一个对象列表,例如运动鞋。我通过解析 JSON 数据来获取这些对象。下面是我用来执行该任务的模型类。

@objcMembers class SneakerInfoTemp: Object, Decodable {
// dynamic var id = UUID().uuidString

//list of dynamic properties that will be deserialized from the json and then passed into realm
dynamic var brand: String?
dynamic var category: String?
dynamic var colorway: String?
dynamic var currentdescription: String?
dynamic var designer: String?
dynamic var imagesrc: String?
dynamic var maincolor: String?
dynamic var name: String?
dynamic var nickname: String?
dynamic var price: String?
dynamic var productlink: String?
dynamic var productlinkhref: String?
dynamic var releasedate: String?
dynamic var silhouette: String?
dynamic var technology: String?
dynamic var webscraperorder: String?
dynamic var webscraperstarturl: String?

enum CodingKeys: String, CodingKey {
case brand, category, colorway, designer, imagesrc, maincolor, name, nickname, price, productlink, productlinkhref, releasedate, silhouette, technology,webscraperorder,webscraperstarturl,currentdescription
}

required init(from decoder: Decoder) throws
{
let container = try decoder.container(keyedBy: CodingKeys.self)

//sneaker information that will be decoded
//made optional due to the fact that some fields don't exist with every shoe
brand = try? container.decode(String.self, forKey: .brand)
category = try? container.decode(String.self, forKey: .category)
colorway = try? container.decode(String.self, forKey: .colorway)
currentdescription = try? container.decode(String.self, forKey: .currentdescription)
designer = try? container.decode(String.self, forKey: .designer)
imagesrc = try? container.decode(String.self, forKey: .imagesrc)
maincolor = try? container.decode(String.self, forKey: .maincolor)
name = try? container.decode(String.self, forKey: .name)
nickname = try? container.decode(String.self, forKey: .nickname)

price = try? container.decode(String.self, forKey: .price)
productlink = try? container.decode(String.self, forKey: .productlink)
productlinkhref = try? container.decode(String.self, forKey: .productlinkhref)
releasedate = try? container.decode(String.self, forKey: .releasedate)
silhouette = try? container.decode(String.self, forKey: .silhouette)
technology = try? container.decode(String.self, forKey: .technology)
webscraperorder = try? container.decode(String.self, forKey: .webscraperorder)
webscraperstarturl = try? container.decode(String.self, forKey: .webscraperstarturl)
super.init()
}


required init()
{
super.init()
}

override class func primaryKey() -> String? {
return "name"
}

required init(value: Any, schema: RLMSchema)
{
super.init(value: value, schema: schema)
}

required init(realm: RLMRealm, schema: RLMObjectSchema)
{
super.init(realm: realm, schema: schema)
}


}

当一切都说完并完成后,我得到了一个对象列表。目前的问题是某些对象是重复的。我尝试使用一组来删除它们,但该组无法区分对象之间的区别。我有点想获取这个对象列表并将它们添加到基于某些属性的集合中,让我们说出名称

我想做这样的事情

   let objectSet = Set((sneakersTemp.sneakers?.brands?.adidas.map { $0.name })!)
print(objectSet.count)

它返回所有唯一的名称,但无论如何我也可以获得所有唯一的对象。

最佳答案

要使用Set删除重复项,类型应符合Hashable

为了简化事情,我们假设 SneakerInfoTemp 类的定义如下:

class SneakerInfoTemp {
let name: String
let price: Double

init(name: String, price: Double) {
self.name = name
self.price = price
}
}

让我们遵守必要的协议(protocol):

extension SneakerInfoTemp: Equatable {
static func == (lhs: SneakerInfoTemp, rhs: SneakerInfoTemp) -> Bool {
return lhs.name == rhs.name
}
}

extension SneakerInfoTemp: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(name)
}
}

因此给定一系列运动鞋:

var array = [SneakerInfoTemp(name: "a", price: 1),
SneakerInfoTemp(name: "b", price: 2),
SneakerInfoTemp(name: "a", price: 3),
]

您可以使用 Set 创建一组唯一元素:

let set = Set(array)

关于swift - 按该值中的特定对象过滤掉对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55576096/

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