Persisting the Items
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 ...
Get iOS Programming: The Big Nerd Ranch Guide, 7th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.