在使用用户日历时,我实现了一个 EKCalendarChooser,它允许用户选择他的多个日历。选定的日历实例被检索得很好。现在我稍后想使用此选择,但如何永久存储它?
我的第一种方法是使用日历标识符并将它们作为字符串数组存储到 UserDefaults 中,例如
@State private var calendarSelection: [EKCalendar]
// my approach to convert the calendar selection into a storable format (string array of ids)
var selectedIds = [String]()
for calendar in calendarSelection {
selectedIds.append(calendar.calendarIdentifier)
}
// now store the string-array, eg. to user defaults:
UserDefaults.standard.set(selectedIds, forKey: "cids")
不幸的是,这不起作用,因为calendarIdentifier
它不是永久标识符,因此会随着时间而改变。正如苹果在其文档中所述:
与日历完全同步将丢失此标识符。你应该有一个计划来处理一个日历,它的标识符不再可以通过缓存它的其他属性来获取。
那么如何存储用户对其日历的选择呢?