Swift Reading and Writing to Plist containing Array of Dictionaries

Viewed 17

I am attempting to get up to speed with Swift coming from an Obj C background and am struggling to read and write Plist files in Swift.

I have two example code snippets opening two plists. One plist uses a dictionary as root, the other plist uses an array as root. For both, I have managed to print out all of the key value pairs but not sure how to access the individual key values for reading and updating.

I don't have a preference in terms of the plist's format, for me the simpler the better. Basically I just need to understand how to access/read/write the individual key values (example: id=1, symbol="BTC", quantity=3) within an array of dictionaries.

Thank you

Example #1 - Root as Dictionary

 var configDict: [String: Any]?
    if let infoPlistPath = Bundle.main.url(forResource: "coins_dictionary", withExtension: "plist") {
        do {
            let infoPlistData = try Data(contentsOf: infoPlistPath)
            
            if let dict = try PropertyListSerialization.propertyList(from: infoPlistData, options: [], format: nil) as? [String: Any] {
                configDict = dict
                print(configDict!)
            }
        } catch {
            print(error)
        }
    }

coins_dictionary.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>coins</key>
        <dict>
            <key>coin</key>
            <array>
                <dict>
                    <key>id</key>
                    <integer>1</integer>
                    <key>symbol</key>
                    <string>BTC</string>
                    <key>quantity</key>
                    <integer>3</integer>
                </dict>
                <dict>
                    <key>id</key>
                    <integer>1027</integer>
                    <key>symbol</key>
                    <string>ETH</string>
                    <key>quantity</key>
                    <integer>17</integer>
                </dict>
            </array>
        </dict>
    </dict>
</plist>

Example #2 - Root as Array

var configArray: [Any]?
    if let infoPlistPath = Bundle.main.url(forResource: "coins_array", withExtension: "plist") {
        do {
            let infoPlistData = try Data(contentsOf: infoPlistPath)

            if let arr = try PropertyListSerialization.propertyList(from: infoPlistData, options: [], format: nil) as? [Any] {
                configArray = arr
                print(configArray!)
               
            }
        } catch {
            print(error)

        }
    }

coins_array.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>id</key>
        <string>1</string>
        <key>symbol</key>
        <string>BTC</string>
        <key>quantity</key>
        <string>3</string>
    </dict>
    <dict>
        <key>id</key>
        <string>2</string>
        <key>symbol</key>
        <string>ETH</string>
        <key>quantity</key>
        <string>42</string>
    </dict>
</array>
</plist>

0 Answers
Related