Encoding and Decoding with Property list — Swift Codable Plist

Posted by Mansi Shah · 06 Jan, 2018 · 2 min read
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! 😀

. . .

1. 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.

import Foundation

public extension FileManager {
// Returns a URL that points to the document folder of this playground.
    static var documentDirectoryURL: URL {
        return try! FileManager.default.url(
            for: .documentDirectory,
            in: .userDomainMask,
            appropriateFor: nil,
            create: false
        )
    }
}

2. Create a Struct MyKey which conforms to Swift 4 Codable Protocol.

struct MyKey: Codable {
    enum Social: String, Codable {
        case twitter, facebook
    }
    init(keyName: String, keyValue: String, type: Social) {
        self.apiKeyName =  keyName
        self.apiKeyValue = keyValue
        self.keyType = type
    }
    let apiKeyName: String
    let apiKeyValue: String
    let keyType: Social
}

3. Create an array of all your API keys that you want to store.

let myKeys = [
  MyKey(
    keyName: "facebookAPIKey", 
    keyValue: "ValueOfMyFacebookAPIKey",
    type: .facebook),
  MyKey(
    keyName: "twitterAPIKey", 
    keyValue: "ValueOfMyTwitterAPIKey", 
    type: .twitter)]

4. Create a separate folder that will contain your Swift Codable plist file containing your key/values. (Only if you wish to)

let documentSubdirectoryURL = URL(
    fileURLWithPath: "MyPlistFolder",
    relativeTo: FileManager.documentDirectoryURL
)
try? FileManager.default.createDirectory(
    at: documentSubdirectoryURL,
    withIntermediateDirectories: false
)

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.

do {
  /// Specify a URL where you want to save the myAPIKeys.plist file with proper extension and pathComponent.
    
  let plistURL = URL(fileURLWithPath: "myAPIKeys", relativeTo: FileManager.documentDirectoryURL.appendingPathComponent("MyPlistFolder")).appendingPathExtension("plist")

  /// Create an instance of PropertyListEncoder()
  /// Specifying outputFormat as `xml` so that you can view it in a source code file format or plist file format.
  /// After that, encode your array of keys that you created above
  /// And finally, write the encoded Data to your myAPIKeys.plist file.
  /// Voila!
  let plistEncoder = PropertyListEncoder()
  plistEncoder.outputFormat = .xml
  let plistData = try plistEncoder.encode(myKeys)
  try plistData.write(to: plistURL)

  /// Create an instance of PropertyListDecoder()
  /// Fetch data from the plistURL
  /// After that, decode your data by specifying the type of your data. Please note that we had created an array of keys and hence the type will be array of MyKey, i.e [MyKey].self
  /// Bingo!
  
  let plistDecoder = PropertyListDecoder()
  let data = try Data.init(contentsOf: plistURL)
  let value = try plistDecoder.decode([MyKey].self, from: data)
} catch {print(error)}

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

. . .

Refer my other articles here:

👉🏼 Animated Transitions

👉🏼 UI Designing for iPhone X

Do show your valuable support by you 👏🏼 to the post! Thanks ☺️

Subscribe to The Friday Brunch
Our twice a month newsletter featuring insights and tips on how to build better experiences for your customers
READ ALSO

Have a product idea?

Talk to our experts to see how you can turn it
into an engaging, sustainable digital product.

SCHEDULE A DISCOVERY MEETING