我試了幾個小時,每種可能的組合,並不能得到它的工作。類型'任何'沒有下標成員與地圖的錯誤
我的功能:
func allItems() -> [TodoItem] {
let todoDictionary = UserDefaults.standard.dictionary(forKey: ITEMS_KEY) ?? [:]
let items = Array(todoDictionary.values)
return items.map({TodoItem(
deadline: $0["deadline"] as! Date,
title: $0["title"] as! String,
UUID: $0["UUID"] as! String!,
description: $0["description"] as! String,
imageSavePath: ($0["imageSavePath"] as! String),
isItComplete: $0["isItComplete"] as! Bool
)}).sorted(by: {(left: TodoItem, right:TodoItem) -> Bool in
(left.deadline.compare(right.deadline) == .orderedAscending)
})
}
給我的錯誤「型 '任何' 無標成員。 我已經研究並試圖改變:
func allItems() -> [TodoItem]
到
func allItems() -> [Any:[TodoItem]]
然而,這引起了那個「類型的任何不符合哈希的協議」的錯誤。這是正確的路線嗎?
請在我的macbook出門前幫忙.....
還沒有一個設計錯誤?字典中帶有鍵/值對的對象。你的映射只適用於每個**值**本身就是一個我懷疑的詞典。 – vadian
你究竟想要返回什麼,數組或字典? – Alexander
這在swift 3.0之前完美運行,所以我不認爲這是一個設計錯誤。目標是返回由DueDate排序的'ToDoItems'數組。 – MartynE23