Skip to content

Instantly share code, notes, and snippets.

@benmorrow
Created April 21, 2015 17:55
Show Gist options
  • Save benmorrow/4b1eab50c27b5694e502 to your computer and use it in GitHub Desktop.
Save benmorrow/4b1eab50c27b5694e502 to your computer and use it in GitHub Desktop.
Alternative to NSUserDefaults write/read to filesystem (fix for glances in WatchKit on physical Apple Watch hardware)
var sharedContainerURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.com.company.project")!.URLByAppendingPathComponent("DB.plist")
func retrieve() -> AnyObject? {
if let data = NSData(contentsOfURL: sharedContainerURL) {
if let unarchived: AnyObject = NSKeyedUnarchiver.unarchiveObjectWithData(data) {
return unarchived
}
}
return nil
}
func save(object: AnyObject) {
let data = NSKeyedArchiver.archivedDataWithRootObject(object)
data.writeToURL(sharedContainerURL, atomically: true)
}
func destroy(){
NSFileManager.defaultManager().removeItemAtURL(sharedContainerURL, error: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment