Skip to content

Instantly share code, notes, and snippets.

@someoneAnyone
Last active March 2, 2016 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save someoneAnyone/82e9534455115f0f0388 to your computer and use it in GitHub Desktop.
Save someoneAnyone/82e9534455115f0f0388 to your computer and use it in GitHub Desktop.
A quick and dirty way of getting dictionary written to the Documents folder for a given app. You can provide a file name or take the default of data. The file name will be appended with ".plist" automatically.
public extension Dictionary where Key: StringLiteralConvertible, Value: AnyObject {
public func saveAsPlist(fileName: String = "data") -> (successful: Bool, path: NSURL?) {
guard let rootPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first else {
print("No documents directory!")
return (false, nil)
}
let url = NSURL(fileURLWithPath: rootPath)
let plistPathInDocument = url.URLByAppendingPathComponent("\(fileName).plist")
let dict = NSDictionary(objects: self.values.map{ $0 }, forKeys: self.keys.map{ String($0) })
let ret = dict.writeToFile(plistPathInDocument.absoluteString, atomically: true)
// print(ret)
//let resultDictionary = NSMutableDictionary(contentsOfFile: plistPathInDocument.absoluteString)
//print("Saved GameData.plist file is --> \(resultDictionary?.description)")
return (ret, plistPathInDocument)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment