Leave a rating/review
Notes: 04. Challenge: URLs
This course was originally recorded in April 2020. It has been reviewed and all content and materials updated as of November 2021.
To go with the string URL you already have, I’m going to challenge you to make your own string and a URL to save it to. Store the string in the challengeString constant.
Any string you like, it’ll do fine! Then, use challengeStringURL to store the URL. The way I’d like you to practice making that is to use the URL initializer, used with remindersDataURL…
…But then, take on the “txt” file extension to the end. You can just name the file the same as the challenge string Have at it!
I had at it. Let’s see if you came up with something similar. Since I’m interested in helping myself to not forget things, I called my string “To Do List”.
let challengeString = "To Do List"
Then I just used the value of challengeString for the file name.
let challengeStringURL = URL(fileURLWithPath: FileManager.documentDirectoryURL, relativeTo: <#T##URL?#>)
Like everything else so far, I used the document directory as a folder for the file:
…relativeTo: FileManager.documentDirectoryURL)
…and then I appended the “txt” extension.
…documentDirectoryURL).appendingPathExtension("txt")
Good work! For completing the challenge, I’ll let you know about a handy property to get the file name and extension from a URL as long as it represents a file: lastPathComponent!
challengeStringURL.lastPathComponent
You’ve now got some practice with URLs and paths. You’ll soon put this to use, but first, a stop to learn about data types in Swift.