下面提到的是我的數組。我需要檢查數組中是否存在條形碼8901058847857或條形碼xxxxxxxxxxx。如何搜索字典數組中的元素?
(
{
barcode = 8901058847857;
image = (
);
name = "Maggi Hot Heads";
productTotal = "60.00";
quantity = 3;
},
{
barcode = 8901491101837;
image = (
);
name = "Lays Classic Salted";
productTotal = "20.00";
quantity = 1;
}
)
我嘗試使用array.contains或array.elements但它不工作,因爲條形碼陣列中的存在。
您需要使用NSpredicate進行搜索。 – KKRocks
你能分享任何相同的例子嗎? –
@RajatAttri在你的情況下,它就像'if let res = yourArray.first(其中:{$ 0 [「barcode」] as?String == searchingCode}){ print(res) } **注意 - **如果數字類型爲'數字'而不是'字符串',則將'barcode'強制轉換爲數字類型 –