我是一個使用swift 3和xcode 8.3的新用戶。目前面臨的一個問題來過濾2陣列/ STRUC凡在控制檯輸出如下:通過特定的映射過濾2個結構/數組
A_List : Optional([117, 115, 18])
B_List : Optional([{
URL = "169.jpeg";
categories = "A";
description = "description XXX";
height = 128;
id = 1;
likes = "1.00";
name = "Cake - Baked";
price = "13.78";
width = 128;
}, {
URL = "1622.jpeg";
categories = "A";
description = "Baked till golden";
height = 128;
id = 15;
likes = "1.00";
name = "Croissant";
price = "3.71";
width = 128;
}, {
URL = "11.jpeg";
categories = "A";
description = "description Crispy.";
height = 128;
id = 18;
likes = "1.00";
name = "Plain";
price = "2.65";
width = 128;
}, {
URL = "1622.jpeg";
categories = "A";
description = "A ";
height = 128;
id = 103;
likes = "1.00";
name = "America Pie";
price = "2.12";
width = 128;
}, {
URL = "11.jpeg";
categories = "B";
description = "Puff";
height = 128;
id = 115;
likes = "1.00";
name = "Puff";
price = "2.12";
width = 128;
}, {
URL = "168.jpeg";
categories = "C";
description = "description YYY";
height = 128;
id = 117;
likes = "1.00";
name = "Normal";
price = "3.18";
width = 128;
}])
我想回到B_List全信息作爲var filtered_List = [AnyObject]()
其中只包含的a_list身份證號碼117, 115, and 18
看起來像如下:
filtered_List : Optional([{
URL = "11.jpeg";
categories = "A";
description = "description Crispy.";
height = 128;
id = 18;
likes = "1.00";
name = "Plain";
price = "2.65";
width = 128;
}, {
URL = "11.jpeg";
categories = "B";
description = "Puff";
height = 128;
id = 115;
likes = "1.00";
name = "Mini Puff";
price = "2.12";
width = 128;
}, {
URL = "168.jpeg";
categories = "C";
description = "description YYY";
height = 128;
id = 117;
likes = "1.00";
name = "Normal";
price = "3.18";
width = 128;
}])
我已經嘗試了幾個代碼,並在YouTube上閱讀教程,但不幸的是沒有找到任何解決方案,它限於swift2示例。
目前,我的代碼試圖如下:
var filtered_List = [AnyObject]()
let fullrList = B_List?.map{$0["id"] as! String}.map{_ in A_List}
filtered_List.append(fullrList as AnyObject)
print("result :\(filtered_List)")
非常讚賞,如果有人高手可以指導或在這裏給您的解決方案。
Swift約定是將lowerCamelCase用於變量名,而不是snake_case,絕對不是Upper_snake_case。此外,您的名字「A list」,「B list」,絕對不會傳達有關其內容的信息。我強烈建議你給出更好的名字 – Alexander
@Peterhdd做**不**使用代碼格式化的東西,而不是像技術的版本或名稱的代碼[見這篇文章](https://meta.stackoverflow.com/ question/254990/when-should-code-formatting-be-used-for-code-text) – jmattheis
@Alexander感謝您的好意提醒。是的,我接受你的評論,並會在我的代碼中做得更好。 –