Saving Data in iOS
May 31 2022 · Swift 5.5, iOS 15, Xcode 13
Part 1: Files & Data
08. Challenge: String Data
Leave a rating/review
Notes: 08. Challenge: String Data
This course was originally recorded in April 2020. It has been reviewed and all content and materials updated as of November 2021.
In this challenge, you’ll learn about what’s happening behind the scenes when you save a
String with its
try string.write(to: <#T##URL#>, atomically: <#T##Bool#>, encoding: <#T##String.Encoding#>)
Here, you’ve got a method that creates a
Data object out of a
String using a “String Encoding”. Create a URL to save your
string from the previous episode using this new method you just saw about, then read the saved
Let’s go over the challenge. Regardless of the outcome, kudos to you for giving it a shot! Creating a URL for your file is something you have done a few times by now.
let catsURL = URL(fileURLWithPath: "Cats.txt", relativeTo: FileManager.documentsDirectoryURL)
How did you do with writing the string to a file, and the
atomically parameter? In your apps,
atomically is an important one; it allows you to write to a URL while avoiding potential file corruption.
…, atomically: true)
For reading the file back, I went with a
let catsChallengeString = try String(contentsOf: catsURL)
Your sidebar should show that you’ve successfully read back your four cat emoji. Excellent!