【发布时间】:2016-12-06 06:41:18
【问题描述】:
我想创建一个字典,其键为日期和事件数组。一个日期可以有多个事件,所以我想将一个日期作为字典的键映射到字符串数组。我将是动态的,日期可以没有事件或日期可以有多个事件。我从日期数组中获取数据,我需要将其与事件映射。
我试过下面的代码:
func addEventToDictionary(eventModal:CalenderEventModal,date:Date) {
var key:String = self.dateFormatter().string(from: date)
if let val = dict_events[key] {
} else {
dict_events[key] = [Any]()
}
dict_events[key] = eventModal
}
这里的 Event modal 是一个 Event 对象。
【问题讨论】:
-
你尝试了这段代码,那么你得到了什么结果,你期望什么?
-
让你的 dict_events = [String : [CalenderEventModal]] ?
-
if let val = dict_events[key] { val.insert(eventModal) }
标签: ios arrays swift dictionary