在Swift3中比较两个不同的结构化数组

时间:2018-04-09 12:35:07

标签: arrays swift xcode

我是IOS和Swift语言的新手,现在我在Swift3工作。

DetailsArray:

[{
    bookId = abcd;
    bookName = "MyBook";
    bookThumbImage = ".jpg"
},
 {
    bookId = efgh;
    bookName = "MyBook1";
    bookThumbImage = "bookefgh.jpg"
},
{
    bookId = ijkl;
    bookName = "MyBook2";
    bookThumbImage = ".jpg"
}
]

当我打印我的现有IdListArray对象时,采用以下给定格式,

IdListArray:

▿ Optional<"NSMutableArray">
    ▿ some : 2 elements
- 0 : abcd
- 1 : ijkl

现在我需要匹配这两个数组(IdListArray&amp; DetailsArray),以从我的DetailsArray中获取匹配的行记录

必需输出:

[{
    bookId = abcd;
    bookName = "MyBook";
    bookThumbImage = ".jpg"
},
{
    bookId = ijkl;
    bookName = "MyBook2";
    bookThumbImage = ".jpg"
}]

谢谢,

1 个答案:

答案 0 :(得分:0)

已编辑:根据您的要求

您可以使用此代码:

    var arrMatchingId:NSMutableArray = NSMutableArray()
    var arrNotMatchingId:NSMutableArray = NSMutableArray()

    for data in arrList{

        let id = (data as! NSDictionary).value(forKey: "bookId")

        if arrID.contains(id!){
            arrMatchingId.add(data) //Id Matched
        }else{
            arrNotMatchingId.add(data)// Id Not Matched
        }
    }
    print(arrMatchingId) // This is the array with matched ID
    print(arrNotMatchingId) //This is the array with Unmatched array