Saving Data in iOS

May 31 2022 · Swift 5.5, iOS 15, Xcode 13

Part 3: Property Lists

20. Challenge: Saving Property Lists

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 19. Property Lists Anatomy Next episode: 21. Challenge: Reading Property Lists

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 20. Challenge: Saving Property Lists

This course was originally recorded in April 2020. It has been reviewed and all content and materials updated as of November 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Add a new method to you TaskStore that also saves your data but instead of using JSON, use property lists. One hint that I can give you is that you have available a Plist equivalent of JSONEncoder. (upbeat music) The first thing I did was create a property to store the URL for the Plist file where the data will be stored. It's identical to the JSON file URL except for the property name and the extension of the file. It now uses "plist" as opposed to "json". Then I added a new method that save the data to a Plist. In it, I copied the entire do-catch statement and just changed the URL to save to. All that remains is to use the right encoder which is PropertyListEcoder. The output format as opposed to being pretty printed is set to XML. You can use OpenStep or binary as alternative formats but while the latter will resolve in smaller file sizes you won't be able to easily use them across different platforms or applications if it's something other than XML. Notice how you don't have to touch anything in your model files. That's because Codeable works with different encoders and decoders. In this case, PropertyListEncoder will take care of things for you so you don't have to make tweaks to your model every time you use a different encoding format. The last thing you want to do is update didSet for the propritizedTask or rate to save using property list and not JSON. Run you app in the simulator, add, delete or modify a task and look at the document direct through your simulator to view the resulting file. Fantastic! You're now able to save your array to a Plist file. Remember when in an earlier video, we mention that your task identifier was always changing whenever we load it or saved or did something to them? Well, that's because we use a UUID that we generate each time and because taskIt used to identify with and this is how SwiftUI and the list knows how to uniquely identify each task within the table and not duplicate them or show them in a funky way. Now we don't really care whether the ID is changing so it's okay if it does that. What's really important is that each task have a unique id that allows us to set it as completed, edit its text or delete it without any duplicates or inconsistencies in our data which this is achieving.