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.
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.documentsURLis pretty convenient!