Swift - Save Dictionary into a plist
<p>I am trying to save/update a dictionary inside a plist file. If a try to save a single value into a plist I have no problem calling the dictionary as a <code>NSMutableDictionary</code> however, if I want to add another dictionary inside it I cannot achieve it.</p>

<p>For example I want to add a dictionary inside the plist with dictionary "dict2" below called "level2" having values as "value1", "value2", "value3" I cannot Achieve this.</p>

<p>Is there a way I can do this?
<a href="https://i.stack.imgur.com/ogGA5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ogGA5.png" alt="enter image description here"></a></p>

<pre><code>   struct MyProgress {

    //1
    enum PlistError: ErrorType {
        case FileNotWritten
        case FileDoesNotExist
    }
    //2
    let name:String
    //3
    var sourcePath:String? {
        guard let path = NSBundle.mainBundle().pathForResource(name, ofType: "plist") else { return .None }
        return path
    }
    //4
    var destPath:String? {
        guard sourcePath != .None else { return .None }
        let dir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
        return (dir as NSString).stringByAppendingPathComponent("\(name).plist")
    }

    init?(name:String) {
        //1
        self.name = name
        //2
        let fileManager = NSFileManager.defaultManager()
        //3
        guard let source = sourcePath else { return nil }
        guard let destination = destPath else { return nil }
        guard fileManager.fileExistsAtPath(source) else { return nil }
        //4
        if !fileManager.fileExistsAtPath(destination) {
            //5
            do {
                try fileManager.copyItemAtPath(source, toPath: destination)
            } catch let error as NSError {
                print("Unable to copy file. ERROR: \(error.localizedDescription)")
                return nil
            }
        }
    }

    //1
    func getValuesInPlistFile() -&gt; NSDictionary?{
        let fileManager = NSFileManager.defaultManager()
        if fileManager.fileExistsAtPath(destPath!) {
            guard let dict = NSDictionary(contentsOfFile: destPath!) else { return .None }
            return dict
        } else {
            return .None
        }
    }
    //2
    func getMutablePlistFile() -&gt; NSMutableDictionary?{
        let fileManager = NSFileManager.defaultManager()
        if fileManager.fileExistsAtPath(destPath!) {
            guard let dict = NSMutableDictionary(contentsOfFile: destPath!) else { return .None }
            return dict
        } else {
            return .None
        }
    }

    //3
    func addValuesToPlistFile(dictionary:NSDictionary) throws {
        let fileManager = NSFileManager.defaultManager()
        if fileManager.fileExistsAtPath(destPath!) {
            if !dictionary.writeToFile(destPath!, atomically: false) {
                print("File not written successfully")
                throw PlistError.FileNotWritten
            }
        } else {
            throw PlistError.FileDoesNotExist
        }
    }

}

class ProgressFunctions {

    static func saveProgressData (plistName: String, dictName:String, dataName:String, dataValue: AnyObject) {

        if let myProgressPlist = MyProgress(name: plistName) {

            let dict = myProgressPlist.getMutablePlistFile()!

            //let savingDict = dict[dictName]![dataName]!

            //dict[dictName] = dataValue

            print(dict)

            do {
                try myProgressPlist.addValuesToPlistFile(dict)
            } catch {
                print(error)
            }

        }

    }
}
</code></pre>