Saving Data in iOS

May 31 2022 · Swift 5.5, iOS 15, Xcode 13

Part 1: Files & Data

06. Saving & Loading Data

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 05. Data & Data Types Next episode: 07. String

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 06. Saving & Loading Data

Foundation Data documentation

This course was originally recorded in April 2020. It has been reviewed and all content and materials updated as of November 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Now that you learned about data types and the relationship between data and bytes, it's time to save the favorite bytes array. In this array, I used all three types of integer literals I just went over. Everything in this column is 240 in decimal. These are all 159. And these are 152. You will be able to make more sense out of what that means in the next video. For now, let's finally save something. First, create a new instance of data. I'll call it "Favorite Bytes Data." All you have to do in the initializer is pass it your way of favorite bytes. You need to know where this data will get stored, so create a URL constant called "Favorite Bytes URL" with the initializer method, which you already learned about, to have it's path be within the user's document directory. Data has a right method that takes a URL. As you see from the compilation error, writing will throw an error if it doesn't succeed so you need to try before hand. And now you know this right succeeded for two reasons. One, you see other readouts in the sidebar down below. If an error was thrown, that wouldn't happen in a playground. And two, you can go to the Favorite Bytes URL. And see that you've got a file written to your document directory. How about it you read it back? To do that, data has an initializer that works with the contents of a URL. You will store the data as "Saved Favorite Bytes Data." If there is no data at the URL you specified, attempting to read would throw an error, so you need to try here as well. The error could happen if your URL represented a directory, for example instead of a file. Note how, in the sidebar, the result that gets printed out is 16 bytes, which also coincides with the value you got in the previous video by using memory layout. Excellent. To get your numbers back, you can just use the array initializer that accepts a data instance. It looks right. Let's have Swift verify that the saved favorite bytes are equal to the original favorite bytes above. They are. You can mostly treat data just like arrays of bytes. So, you can equate two of them directly. And get the same result as equating their conversions to byte arrays.