2017-07-17 72 views
1

我是一個使用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)") 

非常讚賞,如果有人高手可以指導或在這裏給您的解決方案。

+0

Swift約定是將lowerCamelCase用於變量名,而不是snake_case,絕對不是Upper_snake_case。此外,您的名字「A list」,「B list」,絕對不會傳達有關其內容的信息。我強烈建議你給出更好的名字 – Alexander

+0

@Peterhdd做**不**使用代碼格式化的東西,而不是像技術的版本或名稱的代碼[見這篇文章](https://meta.stackoverflow.com/ question/254990/when-should-code-formatting-be-used-for-code-text) – jmattheis

+0

@Alexander感謝您的好意提醒。是的,我接受你的評論,並會在我的代碼中做得更好。 –

回答

0

您應該將所需的ID存儲在Set中,而不是Array中。您只需要一個簡單的filter操作:

let desiredIds: Set = [117, 115, 18] 

B_List.filter{ $0["id"].map{ desiredIds.contains($0) } ?? false } as [AnyObject] 
+0

感謝您的回覆。有用!。無論如何,我如何將A_List設置爲desiredIds?由於A_List會改變。錯誤顯示「無法將類型[AnyObject]的值轉換爲指定的類型」Set「 –

+0

@ D_9268通過'Set'初始化器(例如'Set(someArray)')運行它爲什麼''A_List'類型' [AnyObject]',而不是'[Int]'? – Alexander

+0

感謝你了.A_List是通過其他程序過濾JSON格式的,最後我發現A/B_List中的所有信息都是String。 –

0

感謝大家誰回覆本專題@Alexander。在這裏,我的解決方案可能不像其他人那樣完美。

var resultAnyObject = [AnyObject]() 
var aListIds = [String]() 

for i in A_List! { 
    aListIds.append(i as! String) 
} 

//@Alexander : thanks for code below: 
let desiredIds = aListIds 

let fullList = B_List?.filter{ $0["id"].map{desiredIds.contains($0 as! String) } ?? false }; 

resultAnyObject.append(fullList as AnyObject) 

print("Result of filtered_List :\(resultAnyObject)") 
+0

,您只需要'A_List as [String]',或者'map'。避免重複的「追加」呼叫循環 – Alexander