Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 10, NSUserDefaults Does Not Work

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?

like image 784
antonio Avatar asked Jun 15 '16 15:06

antonio


2 Answers

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.

like image 171
b_ray Avatar answered Oct 11 '22 03:10

b_ray


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.

like image 33
JAL Avatar answered Oct 11 '22 02:10

JAL