App Sandbox: Updating NSUserDefaults fails after deleting app’s container directory
26th February, 2015 —
TL; DR: You forgot to empty the trash.
This is a simple thing but it had me scratching my head for a bit last night: If you delete the container folder of your sandboxed app, you might notice that user defaults at
NSUserDefaults.standardUserDefaults() are not being persisted. This is the same whether or not you call
synchronize() directly (as of 10.8 and later, you shouldn’t be calling it directly anyway)
The reason for it is simple: you didn’t empty the trash and the app has a dangling reference to the
.plist file in the trash.
The solution is also simple: empty the trash.
You can see for yourself by noticing that your application’s property list file is absent as your app runs before you’ve emptied the trash. When you empty the trash and run your app again, it will be created the first time you write to the standard user defaults: