0

我现在开发一个显示 iCloud EKCalendars 列表的 iOS 应用程序。我的快速代码可以获得一组 iCloud EKCalendars,但顺序总是不同。任何人都可以给我建议以使设置井井有条吗?

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
4

2 回答 2

2

let calendars = source.calendars(for: .event)type 也是如此Set<EKCalendar>,根据定义,aSet是未排序的数据类型,因此每当您遍历一个集合时,它总是可能以不同的顺序排列,唯一Set强制执行的是该集合只有一个对象实例。

如果您想订购日历,您必须自己订购,一种是通过title或订购calendarIdentifier

于 2018-07-12T09:46:21.997 回答
0

只是sortSet结果是一个有序数组:

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) }
}
于 2018-07-12T11:02:59.513 回答