Leave a rating/review
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.
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.