我现在开发了一个iOS应用程序,可以显示iCloud EKCalendar的列表。我的swift代码可以获得一套iCloud EKCalendar,但顺序总是不同的。有人能给我建议把布景整理好吗?
let eventStore = EKEventStore()
let sources = eventStore.sources
for source in sources {
if (source.title == "iCloud") {
let calendars = source.calendars(for: .event)
for calendar in calendars {
print("calendar title = " + calendar.title)
}
}
}
代码结果示例:
calendar title = title1
calendar title = title6
calendar title = title5
calendar title = title3
calendar title = title4
calendar title = title2
因此let calendars = source.calendars(for: .event)
的类型是Set<EKCalendar>
,根据定义,Set
是未排序的数据类型,因此无论何时迭代一个集合,它都可能是不同的顺序,Set
唯一强制执行的是对象只有一个实例,即集合。
如果你想订购日历,你必须自己订购,一种是按title
或calendarIdentifier
订购。
只有sort
和Set
,结果是有序数组:
let eventStore = EKEventStore()
let sources = eventStore.sources.filter{ $0.title == "iCloud" }
for source in sources {
let calendars = source.calendars(for: .event).sorted{ $0.title < $1.title }
calendars.forEach { print("calendar title = ", $0.title) }
}