Saving Data in iOS

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

Part 1: Files & Data

01. Introduction

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Next episode: 02. Documents Directory URL

Notes: 01. Introduction

Prerequisite knowledge for this course assumes familiarity with Swift, and being comfortable building a SwiftUI application in Xcode.

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

Transcript: 01. Introduction

Hi, everyone. I’m Jessy. I work with Felipe, who is the instructor of this course. He originally recorded this using the tooling that was available in April 2020.

I’ve gone through this whole course myself, in November 2021, the latest version of Xcode and Swift. And it’s still in perfectly-working condition—this aspect of iOS development has become quite stable.

So, this will be the last you’ll be seeing of me, in this course. But I hope that you’ll join me for some of the other courses I’ve created, later in our learning paths. And now, here is the fantastic Felipe!

Hi friends, it’s Felipe. Or as my friends and relatives now call me, Forgetful Felipe!

Welcome to the Saving Data in iOS Course, where you’ll learn about saving data and working with data that has been saved!

Recently I’m always leaving the house but forgetting something. My keys, my sunglasses, my wallet, I can never seem to remember to take them all with me. I’ve been trying to use my brain to “save” what I need to take every time I go out, but it’s clearly not working.

If you’re coming over from building your awesome reminders app you’ve noticed it too has a problem of “forgetting” things between app launches. How about we tackle both of our problems in this course? You’ll store, or persist as it can also be referred to, your reminders data, and I’ll then have a great tool to not forget things.

Let’s go over the contents of this course. You well learn about: what data is, some basic concepts, and data types. Files, the Filesystem, and the app bundle. FileManager, URLs, and Foundation Data. JSON. Property lists.

For the first part of this course, you won’t be using an Xcode project, or building to iOS devices or the Simulator. Instead you’ll be working in Playgrounds, which are a great way to learn most of the concepts you’ll then use in the latter parts of the course.

At the end of this part, you will learn how to save and load data; techniques that you will then transfer in parts 2 and 3 of the course, when you use your newfound knowledge to store, or persist, your reminders in an iOS application.

Bytes are used to represent data and its different types. If you’re not that familiar with “bytes”, they’re made up of eight “bits”. A “bit” is short for “binary digit”; something we could think of as zero or one.

When an amount of data gets measured, that’s done using bytes. You can’t directly quantify your data without a unit, like “gigabyte” or “terabyte”. It wouldn’t mean anything to say, “I’m going to save a million data today in my app!”

That’s because “data”, in common use, is what’s known as a “mass noun”, or “uncountable noun”. It is the plural form of the word “datum”, but you’ll probably never comes across that form when developing apps.

You can have “some data”, or a “piece of data”. One last note: you can also correctly pronounce Data as “Da-tuh” if you prefer. In iOS, bytes of data can be stored in the Foundation Framework’s Data structure, which will be covered in this course.

What your data represents is also important. A PDF, a text file, and an image, are all files with data, but the way in which they are arranged is what differentiates them.

They are also made up of more basic data types that together create a more complex and robust object.

Some of the data types you will use in this course, and in your applications, are:

  • Integers: To represent whole numbers.
  • Floating Points: To represent decimal numbers.
  • Doubles: Also represent decimal numbers, but with higher precision (basically bigger numbers).
  • Strings: To store text.
  • Booleans: To represent true our false.

The term “saving” is interchangeable with “persistence”. When you close an app, and open it again at a later point, if the data you were working with was saved (without a problem), it will persist, and still be available.

Persisting data involves serializing it, which is a transformation from an in-memory representation of that data, to something more appropriate for storage. You don’t just take a Swift data structure and save it directly. There’s a metamorphosis involved.

The phrase “saving to disk” is used extensively for writing the results of serialization to your device’s flash memory, even in Apple’s own documentation. But there has never been a single iOS device equipped with a physical disk, like iPods originally had.

So technically, the term “saving to drive” is more accurate. But spinning-platter hard disks were a mainstay of the industry for so long, the term “disk” itself has persisted. You can even see this when your SSD-equipped Mac has its main drive named “Macintosh HD”

Before embarking upon your data saving-journey in this course, you should have a good understanding of the Swift programming language.

If you feel lost when watching the next few videos, we’ve got a course on Swift that might help you out. If you can get comfortable with the material in the Programming in Swift course, you should be ready to tackle this one.

In this first part of the course, you’ll get comfortable with files and the FileManager class. Your serialized data will be stored in files within your device’s File System, and you’ll need to manage them.

Their locations will be represented with URLs; Swift has a handy URL type! Then, you’ll get into the details of how your bytes of data get represented in Swift. You’ll be converting back and forth from that binary data and other Swift types.

That’s it for the introduction. Please join me in the next video. There, you’ll be making the necessary preparations to start saving data. See ya there!