Saving Data in iOS

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

Part 1: Files & Data

03. Paths

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: 02. Documents Directory URL Next episode: 04. Challenge: URLs

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: 03. Paths

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

Transcript: 03. Paths

The String components of a URL are collectively known as its “path”. By acting as a wrapper around a String, a URL makes it really easy to manipulate that String for what you need.

Please join me in the “Paths Start” playground. From this point on, the playground in each video is going to start off where the last one ended, except perhaps for a little bit of extra cleanup and organization.

You might have looked over to the right, at your sidebar, and wondered what all that gibberish is about. If you guessed that it’s the path of the Document directory for the playground, you’d be right! But, as this property is a URL, its QuickLook just shows a folder.

Not too informative! If you want to see a URL’s path, you can just use its path property!

FileManager.documentsDirectoryURL.path

A URL’s path is a String, which is more informative in a playground.

But the document directory path is always going to be very long and hard to read. It will also be different for every playground and for every iOS app. What you’re looking at here is not something you’d want to memorize, so you’ll ask FileManager for the right path.

As you saw at the end of the last video, one nice thing about URLs, versus Strings, is that you can right-click them to open them in Finder.

To start off with a playground’s Document directory is completely empty. Let’s create some file URLs so you can remedy that!

Start by deleting all the code in your playground. Then, add a constant called remindersDataURL.

let remindersDataURL = 

Use the URL initializer that has the argument labels fileURLWithPath and relativeTo.

let remindersDataURL = URL(fileURLWithPath: <#T##String#>, relativeTo: <#T##URL?#>)

The path here is the name of the file. Go with “Reminders”.

let remindersDataURL = URL(fileURLWithPath: "Reminders", relativeTo: <#T##URL?#>)

And it’ll be relative to the document directory URL, another way of saying that we’ll put it in the Documents folder.

let remindersDataURL = URL(fileURLWithPath: "mystery", relativeTo: FileManager.documentsDirectoryURL)

Along with the reminder’s data, you’re also going to save a string. Do that at “stringURL”.

let stringURL = 

This time, you’ll construct it not with a URL initializer, but a couple of instance methods that return modified URLs. appendingPathComponent allows you to add a file name to a directory.

let stringURL = FileManager.documentsDirectoryURL
  .appendingPathComponent("String")

That does the same kind of thing as the initializer above. I just wanted to introduce you to both APIs so you are aware of the different options available.

Like in the previous initializer, notice that you’re not using a forward slash in the string in order to represent a path separator. That’s taken care of for you. The same is true of the dot, when you use appendingPathExtension, with “txt” in this case.

.appendingPathExtension("txt")

Let’s check out the path.

stringURL.path

It contains a forward slash and “String.txt” at the end, as expected.