|
Paul mentioned we should think about a generic way to store files to disk using the home directory. Here's what I came up with. I intentionally let the methods throw so that I can deal with the errors "outside" of the call. Of course, wrapping it in do{} catch is possible and return optionals instead, for example. It was not specified for the task.
|
|
Here mine. Looking forward to see how Paul does it.
Call site
|
|
Great solution as well. You catch all the potential errors and throw fatal errors assuming that things "should be in place". I picked the approach that the methods are flexible for use cases when the file might or might not be there and the program does not fail. However, I see one clear difference I would like to ask your input. You do not write the data into the file but convert it to String. Is there any reason to a String being beneficial? I see it more as another "variable" to be aware of as you add UTF8 as a magic constant, for example. |
|
Can someone please explain how the above (either solution) can work if the message.txt already exists? i.e. it was saved using
as Paul did in his demonstration. |
|
Found it in case anyone was wondering the same..my code (it's for loading only):
And is being called like this:
|
|
SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.