19

I try to write/read values to/from [NSUserDefaults standardUserDefaults], but it seems not to work. My code is:

[[NSUserDefaults standardUserDefaults] setObject:@"sampleString" forKey:@"sampleKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSString *defaultsValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"sampleKey"];
return defaultsValue;

This code returns nil, for Xcode 8.0 beta and iPhone 6s (10.0) simulator. However it returns sampleString for Xcode 8.0 beta and iPhone 6s (9.1) simulator

I am testing my app with the new systems. Am I doing something wrong? Or is there a workaround for that? Or is it just a "beta" issue?

6
  • This looks alright to me. Check the documentation for NSUserDefaults and see if somethign is new for iOS 10
    – Gil Sand
    Jun 15, 2016 at 15:56
  • 1
    What is the deep (practical) sense to read a value back right after writing it?
    – vadian
    Jun 15, 2016 at 16:00
  • 1
    @vadian thats not my actual code. I basically write at the first launch of the app and read at the second. The sample code is just a testing after i realized it is not working.
    – antonio
    Jun 15, 2016 at 16:06
  • 3
    [[NSUserDefaults standardUserDefaults] synchronize];... Don't!
    – Helen Wood
    Jun 15, 2016 at 16:13
  • 9
    Some people are marking this question as a duplicate. It is not. The other question is about an IOS 10 API name change / compile-time problem in Swift. This question is about an Objective-C problem at runtime. Jun 15, 2016 at 17:52

4 Answers 4

26

I just had the same problem. There seems to be an issue with NSUserDefaults when first running the iOS8 or 9 simulator and then the iOS10 simulator. Rebooting the mac solved the issue for me.

3
  • 2
    Confirmed. Closing and reopening the simulator didn't work, but rebooting fixed it for me. Jun 15, 2016 at 16:53
  • You can also use an older simulator as a temporary work around (iOS 9.2 worked for me)
    – GK100
    Jun 22, 2016 at 22:16
  • 3
    As per Apple (developer.apple.com/library/prerelease/content/releasenotes/…) WWDC Seed: Running multiple iOS simulators can cause NSUserDefaults to not work Running an iOS 8 or 9 simulator followed by an iOS 10 simulator will cause NSUserDefaults to stop working in the simulator. This can be worked around by rebooting the host Mac.
    – iHS
    Jun 25, 2016 at 9:48
6

This was a bug with the iOS Simulator that shipped with Xcode 8 Beta 1. It has been fixed in Beta 2:

Resolved in Xcode 8 beta 2 – IDE

Simulator

  • NSUserDefaults function correctly in the iOS 10 Simulator, regardless of whether you have previously booted the iOS 8 or iOS 9 Simulator. (25974372)

Xcode 8 Beta 2 Release Notes

Update to Xcode 8 Beta 2 to avoid having to restart your host Mac every time.

3

Update:

If data protection is enabled, synchronize may also fail when an application enter background.


We have this issue on a real device running iOS 10.1.1. Old value could be read, but new change cannot save to disk, synchronize method always fail.

Reboot the device may solve this issue.

5
  • Thanks! I hope this is not the case on the user's device! Dec 6, 2016 at 10:30
  • Thank's you, I'm facing this on real device.
    – Ricardo
    Mar 15, 2017 at 10:27
  • But I have data protection disable.
    – Ricardo
    Mar 15, 2017 at 10:35
  • @Ricardo fail always or just sometimes? It‘s reproducible?
    – BB9z
    Mar 18, 2017 at 7:07
  • @BB9z was my fault
    – Ricardo
    Mar 21, 2017 at 19:46
1

I just had the same problem. There seems to be an issue with NSUserDefaults when first running the iOS8 or 9 simulator and then the iOS10 simulator. Rebooting the mac solved the issue for me. shareeditflag Confirmed. Closing and reopening the simulator didn't work, but rebooting fixed it for me.

As per Apple (apple link ) WWDC Seed: Running multiple iOS simulators can cause NSUserDefaults to not work Running an iOS 8 or 9 simulator followed by an iOS 10 simulator will cause NSUserDefaults to stop working in the simulator. This can be worked around by rebooting the host Mac.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.