我已根据日期对数据进行分组,并将值附加到
AppointmentDateModel
- tableValues。
由于数据包含一个相同的日期和多个约会数据,因此我将它们分组。
但是当我附加值时,它会自动排序或重新排列顺序。
如何设置字典中的顺序以使顺序无法更改?或者我是否以正确的方式对它们进行分组?
我的主要模型是
DownloadEntity
和appointmentDownloadModel
,然后对于分组我使用AppointmentDateModel
声明:
var mDownloadEntity: [DownloadEntity] = []
var tableValues:[AppointmentDateModel] = []
var dict : [Int64:[AppointmentDownloadModel]] = [:]
附加代码:
mDownloadEntity.forEach { apDate in
print(apDate)
var value = dict[apDate.appdate]
if(value == nil){
dict.updateValue([apDate.appointmentDownloadModel!], forKey: apDate.appdate)
}else{
value?.append(apDate.appointmentDownloadModel!)
dict.updateValue(value!, forKey: apDate.appdate)
}
}
dict.forEach{map in
let appointmentDateModel = AppointmentDateModel(appointmentDate: map.key, entities:map.value)
tableValues.append(appointmentDateModel)
}
型号:
struct AppointmentDownloadModel: Codable{
var appointmentModel: Appointment
var progress: Int = 0
var failedList: [Int: String] = [:]
var isFinished: Bool = false
}
struct DownloadEntity: Codable {
let id: Int
var appdate: Int64
var userId: Int
var dFirmId: Int
var appointmentId: Int
var appointmentDownloadModel: AppointmentDownloadModel?
}
struct AppointmentDateModel: Codable {
let appointmentDate:Int64
var entities:[AppointmentDownloadModel]
}
对于有序字典,您可以添加 Swift 包管理器:swift-collections 并添加 import OrderedCollections 并使用 OrderedDictionary 对象