|
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:
|
|
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.