Last active
March 2, 2016 16:13
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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