Learning about data technologies on iOS?
I am the author of Core Data Fundamentals with Swift, CloudKit, iOS Data Persistence: The Big Picture, and eight other courses on Pluralsight.
Deepen your understanding by watching the courses!
I am the author of Core Data Fundamentals with Swift, CloudKit, iOS Data Persistence: The Big Picture, and eight other courses on Pluralsight.
Deepen your understanding by watching the courses!
Swift Tip – Accessing a User’s Documents Directory with URL Extension
When you need to access the documents directory on a user’s device, what do you do?
In the past, I know I’ve gotten used to typing out the same set of code over and over throughout my apps. I know, I know – DRY – don’t repeat yourself.
One way to simplify this is to use a Swift extension to URL
. Doing this will help you centralize where this code is located in your app, and keep your code DRY. If you need to access the documents directory in more than one spot, you’re just a URL
property call away from it.
Extension implementation
|
|
Extension usage
Here’s an example of using the extension during the routine to create the Core Data stack. For that process, you need to grab a URL to where your SQLite database file will be located. Take a look:
|
|
URL.documentsURL
is pretty convenient!