Encoding and Decoding with Property list — Swift Codable Plist
Why do you need a Property List (Plist) Decoder & Encoder?
Sometimes in our application, we need to store the value of some of the API Keys of third party Integrations (if added) like Facebook, Twitter, etc. (or some static data source for your table/collection view).
One of the ways to put these keys and values is adding it in a Property List file, aka Plist file. In this post, I’ll show you how I did the same using Swift Plist Encoder and Decoder — PropertyListEncoder & PropertyListDecoder, in one of my projects! 😀
- Just for convenience, I have added an extension to FileManager that has a variable that returns a URL that points to the document folder of your playground.
2. Create a Struct MyKey which conforms to Swift 4 Codable Protocol.
3. Create an array of all your API keys that you want to store.
4. Create a separate folder that will contain your Swift Codable plist file containing your key/values. (Only if you wish to)
5. As we dive into the Encoding/Decoding Property List online using PropertyListEncoder and PropertyListDecoder, we might encounter several try-catch, so start with a do block.
From the value, you can retrieve your specific key’s name/value by filtering on the keyType 😀
P.S. This guide only describes the “how to” encode and decode plist files using ‘propertylistencoder’ and ‘propertylistdecoder’. There are many alternatives that can be used for fetching the API Key Values, saving and fetching static data for table/collection view, etc.
⭐️ If you want to check out the playground page, here you go:
Go for the “MyPlistPlayground.playground”
Do show your valuable support by you 👏🏼 to the post! Thanks ☺️