基于Swift中的struct属性删除数组中的重复结构 [英] Remove duplicate structs in array based on struct property in Swift

查看:352
本文介绍了基于Swift中的struct属性删除数组中的重复结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个简单的结构并实现了Equatable协议:

I have made a simple struct and implemented the Equatable protocol :

extension MyModelStruct: Equatable {}

func ==(lhs: NModelMatch, rhs: NModelMatch) -> Bool {
    let areEqual = lhs.id == rhs.id
    return areEqual
}

public struct MyModelStruct {

    var id : String?
    var staticId : String?

    init(fromDictionary dictionary: NSDictionary){
        id = dictionary["id"] as? String
        ...
}

然后在我的项目中我得到一个数组[MyModelStruct],我要做的是删除所有具有相同id的MyModelStruct

Then in my project i get an array of [MyModelStruct], what i what to do is to remove all the MyModelStruct that have the same id

let val1 = MyModelStruct(id:9, subId:1)
let val2 = MyModelStruct(id:10, subId:1)
let val3 = MyModelStruct(id:9, subId:10)

var arrayOfModel = [val1,val2,val3]; // or set but i do not know how to use a set
var arrayCleaned = cleanFunction[M2,M3] 

我如何制作cleanFunction?

How can i make the cleanFunction ?

有人可以帮忙吗。
谢谢大家。
Xcode:版本7.3.1

Can someone help please. Thanks for all. Xcode : Version 7.3.1

推荐答案

我真的不希望别人只是回答,因为它是只有一个,这就是为什么我告诉你如何使用套装的力量。无论在哪里,都可以使用集合,无论是否存在。集提供快速方法来检查元素是否在集合中(包含),删除元素( remove ),合并两组( union )等等。通常人们只是想要一个阵列,因为他们熟悉它,但通常一套就是他们真正需要的。话虽如此,以下是如何使用集合:

I really don't want people to just take an answer because it's the only one, that's why I'm showing you how you can use the power of sets. Sets are used wherever it doesn't make sense to have more than one, either it's there or not. Sets provide fast methods for checking whether an element is in the set (contains), removing an element (remove), combining two sets (union) and many more. Often people just want an array because they're familiar with it, but often a set is really what they need. With that said, here is how you can use a set:

struct Model : Hashable {
    var id : String?

    var hashValue: Int {
        return id?.hashValue ?? 0
    }
}

func ==(l: Model, r: Model) -> Bool {
    return l.id == r.id
}

let modelSet : Set = [
    Model(id: "a"),
    Model(id: "hello"),
    Model(id: "a"),
    Model(id: "test")
]

// modelSet contains only the three unique Models

一个类型的唯一要求是在一个集合中 Hashable 协议(扩展 Equatable )。在您的情况下,您只需返回 String 的基础 hashValue 。如果您的ID始终是一个数字(可能是它),您应该将 id 的类型更改为 Int ,因为 String s的效率远低于 Int s,并且使用<$没有意义c $ c>字符串。

The only requirement for a type in order to be in a set is the Hashable protocol (which extends Equatable). In your case, you can just return the underlying hashValue of the String. If your id is always a number (which it probably is), you should change the type of id to be an Int, because Strings are much less efficient than Ints and it doesn't make sense to use a String.

还考虑将此属性存储在某处,以便每次收到新模型时,您都可以

Also consider storing this property somewhere, so that every time you receive new models, you can just do

modelSet.unionInPlace(newModels)

这篇关于基于Swift中的struct属性删除数组中的重复结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆