根据'id'过滤数组

时间:2017-05-21 12:07:47

标签: arrays swift

我有一系列的友情ID&组ID。从这里我必须过滤匹配的朋友和具有来自我的其他数组的ID的组,其中包含朋友,组。

    let friendIDS = ["DW548ADS456", "DSAD86465"]
    let groupIDS = ["RQ13131"]

现在如何过滤我的项目,以便我将拥有Group&拥有以上匹配ID的朋友

我是通过这种方式尝试的,但不知道如何在该过滤器中搜索匹配的ID

var items = [Conversation]()

    let friendIDS = ["DW548ADS456", "DSAD86465"]
    let groupIDS = ["RQ13131"]
    self.filtereditems =  self.items.filter {
        $0.groupDetails?.groupID == ""
        $0.user?.userId == ""
    }

会话课

class Conversation {

    //MARK: Properties
    let user: User?
    let groupDetails: GroupDetails?
}


struct User:  JSONDeserializable {

    var userId: String = ""
init(userId: String)
        self.userId = userId
    }

class GroupDetails: NSObject {


    //MARK: Properties
    let groupID: String
    init(groupID: String){
        self.groupID = groupID
}

1 个答案:

答案 0 :(得分:1)

如果我做对了,

 struct user {
     var groupID: String
     var userID: String
 }

 let items = [
     user.init(groupID: "RQ13131", userID: "DSAD86465"),
     user.init(groupID: "1", userID: "DW548ADS456"),
     user.init(groupID: "2", userID: "DW548ADS456")
]
 let friendIDS = ["DW548ADS456", "DSAD86465"]
 let groupIDS = ["RQ13131"]

 let filtereditems =  items.filter {
        groupIDS.contains($0.groupID) ||
        friendIDS.contains($0.userID)
    }

print(filtereditems)

打印

  

[user(groupID:“RQ13131”,userID:“DSAD86465”)]