[英]Compare Two Different Structured Arrays in Swift3
我是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和DetailsArray),以從我的DetailsArray中獲取匹配的行記錄
要求的輸出:
[{
bookId = abcd;
bookName = "MyBook";
bookThumbImage = ".jpg"
},
{
bookId = ijkl;
bookName = "MyBook2";
bookThumbImage = ".jpg"
}]
謝謝,
您可以使用以下代碼:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.