Samuel Hill
Samuel Hill

Reputation: 186

How do I access the data in this Plist?

I've been trying to grab this data, load it into two arrays, and eventually pass those into Swift-Linechart. Currently, I'm using this to load the data:

func loadUserData(){
        var myDict: NSDictionary?

        if let path = NSBundle.mainBundle().pathForResource("UserData", ofType: "plist") {
            if let dict = NSDictionary(contentsOfFile: path) as? Dictionary<String, AnyObject> {
                userData = dict
            }
        }
    }

Once I have the userData in a dict, how to I get it into a CGFLoat array of values so I can graph it?


UserData.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>Triggers</key>
    <dict>
        <key>Alcoholic Drinks</key>
        <array>
            <array>
                <date>2014-12-03T16:28:53Z</date>
                <string>3</string>
            </array>
            <array>
                <date>2014-12-04T16:29:33Z</date>
                <string>0</string>
            </array>
        </array>
        <key>Caffeinated Drinks</key>
        <array>
            <array>
                <date>2014-12-03T16:30:22Z</date>
                <string>2</string>
            </array>
            <array>
                <date>2014-12-04T16:30:02Z</date>
                <string>1</string>
            </array>
        </array>
    </dict>
    <key>Symptoms</key>
    <dict>
        <key>Redness</key>
        <array>
            <array>
                <date>2014-12-03T16:25:21Z</date>
                <string>0.75</string>
            </array>
            <array>
                <date>2014-12-04T16:25:51Z</date>
                <string>0.5</string>
            </array>
        </array>
        <key>Swelling</key>
        <array>
            <array>
                <date>2014-12-03T16:27:00Z</date>
                <string>.6</string>
            </array>
            <array>
                <date>2014-12-04T16:27:52Z</date>
                <string>.2</string>
            </array>
        </array>
    </dict>
</dict>
</plist>

Upvotes: 0

Views: 153

Answers (1)

Daniel T.
Daniel T.

Reputation: 33967

Assuming you turn the strings in your plist to numbers. This will extract the Symptoms->Redness data out:

func parseRednessData() {
    let symptoms = userData?["Symptoms"] as [String: AnyObject]
    let redness = symptoms["Redness"] as [[AnyObject]]
    let floats = redness.map { $0[1] as Float }
    println(floats)
}

Use the above as an example for extracting other bits of data.

EDIT

I noticed that in my original code, floats was actually an array of AnyObject. I fixed that.

Upvotes: 1

Related Questions