我有一些数据数组,首先,我将字符串日期值转换为日期类型,然后创建一个字典,其中这些相同的日期作为键。但我需要的值不是从中获取这些日期的模型,而是一组相应的行(标题)。我怎样才能实现这个?
static var newModels = [NewNewsRecordModel]()
static var sortedMassWithSections = [SectionModel]()
static var models: [NewsRecordModel]
func translate() {
for model in Transducer.models {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
if let date = dateFormatter.date(from: model.publishedAt) {
let newModel = NewNewsRecordModel(title: model.title, publishedAt: date)
Transducer.newModels.append(newModel)
}
}
}
func sortingByDictionaryToMass() {
let calendar = Calendar.current
let dict = Dictionary(grouping: Transducer.newModels) { (model) -> Date in
let components = calendar.dateComponents([.year, .month, .day], from: model.publishedAt)
let finalDate = calendar.date(from: components)
return finalDate!
}
for element in dict {
// Need to translate values from NewNewsRecordModel into String
let sectionModel = SectionModel(sectionTitle: element.key, cell: element.value)
Transducer.sortedMassWithSections.append(sectionModel)
// Need to sort sortedMassWithSections by first element in two-dimensional array
}
}
struct NewNewsRecordModel {
let title: String
var publishedAt: Date
}
struct SectionModel {
let sectionTitle: Date
let cell: [NewNewsRecordModel]
}
struct NewsRecordModel {
let title: String
var publishedAt: String
}