Documents Directory URL | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429634-saving-data-in-ios/lessons/2
1 Like

TIL that the backtick is a thing here – but instead of using backticks to get around the keyword, you could also use Self.default (as its a static var) – I find that to be more readable…

Thank you for your comment. I’m happy to know you now have backticks as part of your Swift tool belt.

Feel free to use Self.default if that makes things more readable for you. The goal was mostly to keep the code as succinct as possible while also showcasing a cool, oftentimes obscure, Swift feature :slight_smile:

There may be scenarios where you need to use the backticks in your code if no alternative is available, perhaps when making a method named guard or weak. In any case, the options are there for you to use in your codebase :slight_smile:

That totally makes sense and you definitely reached your goal by letting me know that backtcks are a thing. :grinning:

I have a question here. Why did we make ‘documentsDirectoryURL’ as a computed property and not a simple constant property since the url is never going to change and we need not compute it every time we access it? Please correct me if I am wrong.

The answer, in this case, is that there’s no particular reason why we chose a static read-only computed property versus a static constant stored property :slight_smile:

We already deal with a call to a function so as it stands we’re not losing any performance by adding a property around this logic. We could see some minor benefit by using a constant store property, but again it’s likely of minimal performance benefit and for the usage we give it in this example then either approach would be ok :slight_smile:

Hi, thanks for a great class. I have a question that also incorporates some material from later in the course, but I think this is the most logical video to attach this question to.

My question is - on iOS, where would you write the file to make it visible in the Files app? I’m testing code in the simulator that writes relative to

`default`.urls(for: .documentDirectory, in: .userDomainMask)[0]

and the file shows up on my Mac’s disk under

/Users/blah/Library/Developer/CoreSimulator/Devices/...

but I can’t see it on the simulator itself by browsing around in the Files app. What I’m trying to do, by the way, is let the user export data from internal CoreData storage to JSON so they can do what they like with it - I have all the code to export and I can get it into the .documentDirectory, but I don’t quite know how to get it somewhere the user could see it.

I don’t currently have a paid developer account, and I’m not signed into iCloud Drive on the Simulator. I plan in the near future to take care of that and sync the data with iCloud, but I’m hoping I could at least write to the device without that.

Thanks for any thoughts on this!

Perhaps this tutorial can help you get started with connecting your app to the Files app:

Let us know if that helps, and happy coding! :smiley:

1 Like

This looks fantastic, thanks!

Hi there! I see that the playground file is in .xcplayground format instead of .playground format. unable to work on any of those files. Attaching the screenshot, Any help would be highly appreciated!

These playground files seem to be outdated. I can open the directories like regular Xcode projects, but it doesn’t load the interactive playground. Please update these so we can follow along with the course! Thanks

Hey @varunkashyap07 - I agree these should be updated, but as a short-term solution you can create a new (up-to-date) playground file in Starter by right-clicking the in the Xcode file explorer and selecting File → New → Blank Playground. That way you can at least follow along with the video.

Hey @ozymoondias, I have tried that too. when you try to add a new file in the starter project, it opens up just as a new file, there is no option to create a playground file inside the project. However, I could open a new independent playground file outside the project. I was browsing through some of the other iOS videos and noticed that they are in the same wrong format. These files need a serious update, being a pro customer I wouldn’t expect this kind of service.