April 2020
Intermediate to advanced
528 pages
15h 40m
English
Now for the “how.” To write data to the filesystem, you call the method write(to:options:) on an instance of Data. The first parameter indicates a location on the filesystem to write the data into, and the second parameter allows you to specify options that customize the writing behavior.
In ItemStore.swift, update saveChanges() to write out the data to the itemArchiveURL.
Listing 13.7 Writing data to disk (ItemStore.swift)
func saveChanges() -> Bool {
print("Saving items to: \(itemArchiveURL)")
do {
let encoder = PropertyListEncoder()
let data = try encoder.encode(allItems)
try data.write(to: itemArchiveURL, options: [.atomic])
print("Saved all of the items")
return true } catch let encodingError { print("Error ...Read now
Unlock full access