NSUserDefaults日志中的错误

我在日志中收到一些错误消息

[User Defaults] Failed to write value for key GameId in CFPrefsPlistSource (Domain: xxx.xxxxxx, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null)): Path not accessible, switching to read-only [User Defaults] attempt to set  for key in  in read-only (due to a previous failed write) preferences domain CFPrefsPlistSource (Domain: xxx.xxxxxx, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null)) 

是什么导致了这个?

这就是我使用NSUserDefaults

 - (NSString *)gameId { if (_gameId) return _gameId; _gameId = [[NSUserDefaults standardUserDefaults] objectForKey:@"GameId"]; return _gameId; } - (void)setGameId:(NSString *)aGameId { _gameId = aGameId; [[NSUserDefaults standardUserDefaults] setObject:_gameId forKey:@"GameId"]; [[NSUserDefaults standardUserDefaults] synchronize]; } 

很多人在Xcode 8上发现了这个问题。

“虽然听起来很疯狂,但尝试重新启动手机和主机,有时Xcode可能因为总和原因而卡住,只有重启才有帮助。

使用Xcode 8.1 Beta构建,您将看到相同的警告,但您也将获得该值。 “

参考: https : //forums.developer.apple.com/thread/51348

我收到了类似的错误:

 [User Defaults] attempt to set  for key in  in read-only (due to a previous failed write) preferences domain CFPrefsPlistSource<0x1700f5f80> 

但它只发生在我插入Xcode时。

设备重启修复此问题。

如果您尝试使用userDefault将数据保存到扩展APP时遇到此问题,可能您已编写此代码:[[NSUserDefaults standardUserDefaults ] initWithSuiteName:@“group.xxx.com”] ;,此代码重置默认userDefault。 实际上,正确的代码是:[[NSUserDefaults alloc ] initWithSuiteName:@“group.xxx.com”]; http://www.jianshu.com/p/e782104c3bc3

我刚才遇到了同样的错误。 我的错误是因为我多次调用同步。

一旦所有错误消息消失,只需调用[userDefaults synchronize]。

我刚刚删除了应用程序并通过Xcode再次安装。 使用XCode 8.2.1。

我想分享我对这个问题的解决方案。 对于我的情况,错误是合法的错误,我的数据没有正确保存。 事实certificate,错误是由一个循环中的NSUserDefaults“数据保存”引起的。

通过“数据保存”我的意思是这个(伪代码):

 loop{ [defaults setObject:@"Data" forKey:@"Key"]; [defaults synchronize]; } 

因此,代码会非常快速地执行多次并导致问题。 我猜“synchronize”无法处理并发调用。

需要做的是:

 loop{ [defaults setObject:@"Data" forKey:@"Key"]; } [defaults synchronize]; 

在设置所有对象后调用一次同步。