好吧,那麼,快速noob警報:Swift 3迭代嵌套的Dictionary?
如何做最簡單的迭代給予以下數組(我不知道該稱爲這種形狀:數組,字典,對象...)?
func showNotification(_ sender: [AnyHashable: Any]) { ... }
sender["actions"]:
Optional([{"text":"Confirm","type":"response"},{"text":"Decline","type":"response"},{"link":"https://www.stackoverflow.com","text":"Website","type":"info"}])
嘗試:
if let result = sender["actions"] {
print("YES \(result)")
for action in result as! [String] {
print(action)
}
}
the above prints
:
YES [{"text":"Confirm","type":"response"},{"text":"Decline","type":"response"},{"link":"https:\/\/www.stackoverflow.com","text":"Website","type":"info"}]
...然而,返回以下錯誤:
Could not cast value of type '__NSCFString' (0x1a7c28d50) to 'NSArray' (0x1a7c297c8)
這裏的最終目標是簡單地去行動的每一個單獨的,即:
{"text":"Confirm","type":"response"}
{"text":"Decline","type":"response"
等...
確實斯威夫特有map
功能。 FYI Im來自Java和JavaScript世界... swiftyjson
似乎有點重一個循環。
謝謝,並一如既往的幫助和指導表示讚賞!
編輯:
這是通過傳遞給函數sender
帕拉姆打印:
sender: [AnyHashable("title"): title!, AnyHashable("message"): message, AnyHashable("message_id"): 0:1503511875428318%03300c3203300c32, AnyHashable("id"): 1497708240713, AnyHashable("actions"): [{"text":"Confirm","type":"response"},{"text":"Decline","type":"response"},{"link":"https:\/\/www.notifyd.com","text":"Website","type":"info"}], AnyHashable("aps"): {
"content-available" = 1;
}]
Swift does have map – ryantxr
這個問題很混亂。請清楚顯示'print(「YES \(result)」)的輸出。 – rmaddy
不,我仍然無法循環嵌套的字典。 「Swift是否有地圖功能」是一個替代選擇的建議,而不是問題。 – studiobrain