通过该值中的特定对象过滤出对象

时间:2019-04-08 14:47:22

标签: swift

所以我有一个对象列表,例如运动鞋。我通过解析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)

它返回所有唯一名称,但无论如何我还是要获取所有唯一对象。

2 个答案:

答案 0 :(得分:0)

使用.filter代替.map。试试这个

let array : [Obj] = sneakersTemp.sneakers?.brands?.adidas
let filtered = filterUnique(array: array)

其中Obj –您的sneakersTemp.sneakers?.brands?.adidas数组的类/结构(或其具有 var name:String 的超类)

func filterUnique(array : [Obj]) -> [Obj] {
    var filtered : [String] = []
    let objectSet = array.filter({ obj -> Bool in
        if filtered.contains(obj.name) {
            return false
        }
        filtered.append(obj.name)
        return true
    })
    print(objectSet.count)
}

答案 1 :(得分:0)

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

为简化起见,我们假设SneakerInfoTemp类的定义如下:

class SneakerInfoTemp {
    let name: String
    let price: Double

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

我们遵守必要的协议

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)