Hello, in this swift 5 tutorial we will use the Codable protocols, File Manager, and methods on Data to persist information
between app launches. We will develop an iOS app
called Emoji Dictionary, in which the user enters
a series of new series of emojis. And we'll save that series of emojis
even after the app has been closed. We will write information
to a file on the disk, explore encoding and decoding,
and use the file manager. We will create an Archive URL and write to a file in the documents plist.
This lab is part of Swift Development Data Collections 4.7 EmojiDictionary (Emoji Dictionary).
I am using Xcode 14 | Swift 5 | macOS 13 Ventura | iOS 16
Timestamps:
00:00 - App Demo and Swift Data Persistence Intro
01:21 - Creating ArchiveURL
04:40 - Implementing loadFromFile
06:51 - Implementing saveToFile
08:30 - Implementing loadSampleEmoji
08:53 - EmojiTableViewController
11:50 - App End Demo
Here are the steps we will implement:
Step 1
Implement Saving on Emoji
To persist your app's information across app launches, you need to be able to write its information to a file on disk. Start by making your Emoji struct adopt the Codable protocol in its declaration.
Remember that you can implement saving and loading as static methods on the model. Add the static method signature for a saveToFile(emojis:) function in the Emoji class. It should take an array of Emoji objects as a parameter. Now add a static loadFromFile() method. It shouldn't take any parameters, but should return an array of Emoji objects.
In each of these methods, you'll use either a PropertyListEncoder to encode your Emoji object or a PropertyListDecoder to decode it. But to write data to a file, you need to have a file path. Add a static property archiveURL to your Emoji class that returns the file path for Documents/emojis.plist. If you need a refresher on how to do this, go back and read this lesson's section on sandboxing.
Now that you have a path for saving your Emoji object, fill in the method bodies of saveToFile(emojis:) and loadFromFile(), using Emoji.archiveURL as the file path. Your saveToFile(emojis:) method should save the supplied emojis array by encoding it and then writing it to Emoji.archiveURL. Your loadFromFile() method should read the data from the file, decode it as an [Emoji] array, and return the array.
Create a static sampleEmojis method that returns a predefined [Emoji] collection. You can use the list assigned to emojis in EmojiTableViewController as your list of items.
Step 2
Save and Load When Appropriate
Update emojis to be initialized to an empty collection rather than a large sample collection. When the viewDidLoad() method is called, you should check the Documents directory for any previously saved Emoji objects using loadFromFile(). If they're found, assign them to emojis. If not, assign emojis to Emoji.sampleEmojis.
Take a moment to think about when it might be appropriate to save your Emoji objects.
In this case, the central spot for your data is the emojis array on the EmojiTableViewController—which means it would be appropriate to call saveToFile(emojis:) whenever the emojis property is changed.
Next, think about when it might be appropriate to load your archived Emoji objects. Again, in this simple case, there's really only one point where the archived data will need to be unarchived—when the first view loads. You should already be calling this method in the first view controller's viewDidLoad().
By now, your emoji data should be properly saving and loading. Run your app. Try adding a new emoji and tapping the Save button. Then close the app, reopen it, and observe whether the emoji persists in the table view. Repeat this process for editing an already existing emoji.
Please SUBSCRIBE, LIKE, & SHARE for more videos and to stay updated with the latest important videos: https://www.youtube.com/c/CloudDataSc...
~-~~-~~~-~~-~
Please watch next: "Databricks Zero to Hero! - Session 1 | What is Databricks? | Databricks Tutorial"
• Databricks Zero to Hero! - Session 1 ...
~-~~-~~~-~~-~