Created
October 19, 2019 05:54
-
-
Save laevandus/14442007d2d16cc82a9b35170613bb97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let container = Container<CalendarKeys>() | |
let event1 = EventItem(date: Date(), title: "title1", description: "description1") | |
container.add(event1, key: .homeEvents) | |
let event2 = EventItem(date: Date(), title: "title2", description: "description2") | |
container.add(event2, key: .workEvents) | |
let note1 = NoteItem(text: "text3") | |
container.add(note1, key: .notes) | |
let homeEvents: [EventItem] = container.items(forKey: .homeEvents) | |
let workEvents: [EventItem] = container.items(forKey: .workEvents) | |
let notes: [NoteItem] = container.items(forKey: .notes) | |
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("Test") | |
do { | |
try container.write(to: url) | |
} | |
catch { | |
print(error as NSError) | |
} | |
do { | |
let restoredContainer = try Container<CalendarKeys>(contentsOfURL: url) | |
let homeEvents: [EventItem] = restoredContainer.items(forKey: .homeEvents) | |
let workEvents: [EventItem] = restoredContainer.items(forKey: .workEvents) | |
let notes: [NoteItem] = restoredContainer.items(forKey: .notes) | |
print("Home events: ", homeEvents) | |
print("Work events: ", workEvents) | |
print("Notes: ", notes) | |
} | |
catch { | |
print(error as NSError) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment