NSUserDefaults no longer saving values

I seem to be having problems using the following API. The values are not being saved at all.


      NSUserDefaults.standardUserDefaults().setValue(stringVal, forKey:Constants.iCloudPreferenceSelected)
   
      NSUserDefaults.standardUserDefaults().synchronize()
       
      let isUserICloudChoiceSelected = NSUserDefaults.standardUserDefaults().stringForKey(Constants.iCloudPreferenceSelected)



Is there a bug in 10.11 ??

Replies

I'm not aware of any bugs quite like that open currently. If you have a project that can reproduce this, please file a bug report (bugreport.apple.com) and attach it, and I'll have a look with a debug CoreFoundation. Thanks! 🙂


As an aside, within a single process like this, calling synchronize is generally unnecessary; you only need it if you're about to call exit(), or about to notify another process to read preferences.

I'm not sure if it would be considered expected behavior (because of sandboxing) or not, but NSUserDefaults doesn't seem to save values within an iOS playground (using Xcode 7 beta 2, on 10.10.3).


In an OS X playground (and in the iOS simulator) the following code works fine, but in an iOS playground the retrieved value will always be nil:

let defaults = NSUserDefaults.standardUserDefaults()

var infoIn = "test_string"

defaults.setObject(infoIn, forKey: "test_key")
defaults.dictionaryRepresentation()

var rawOut = defaults.objectForKey("test_key")
var infoOut: String? = rawOut as? String

Maybe this is the same issue LCS pointed out in that other thread - incorrectly using setValue:forKey: instead of setObject:forKey:.

this is definiatley a bug - we use this for our app www.odrive.com for finderSyncExtension and our app to share data in a shared application group and it works perfectly on yosemite - has been for close to a year now and it will not work on el capitan. i filed a bug 22488279.