xcode 日志中的 NSUserDefaults 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39876042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSUserDefaults errors in logs
提问by Valentin Shamardin
I get some error messages in logs
我在日志中收到一些错误消息
[User Defaults] Failed to write value for key GameId in CFPrefsPlistSource<0x1740faf00> (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<0x1740faf00> (Domain: xxx.xxxxxx, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null))
[用户默认值] 无法在 CFPrefsPlistSource<0x1740faf00> 中写入键 GameId 的值(域:xxx.xxxxxx,用户:kCFPreferencesCurrentUser,ByHost:否,容器:(空)):路径不可访问,切换为只读
[用户默认值] 尝试在只读(由于之前的写入失败)首选项域 CFPrefsPlistSource<0x1740faf00>(域:xxx.xxxxxx,用户:kCFPreferencesCurrentUser,ByHost:否,容器:(空))中设置键入
What does cause this?
这是什么原因造成的?
That's how I use NSUserDefaults
:
这就是我使用的方式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];
}
回答by Tushar
Lot of people are finding this issue on Xcode 8.
很多人在 Xcode 8 上发现了这个问题。
"As crazy as it sounds try rebooting phone and host, sometimes Xcode can get stuck for sum reason only restart helps.
“尽管尝试重新启动手机和主机听起来很疯狂,但有时 Xcode 会因为总和原因而卡住,只有重新启动才有帮助。
build with Xcode 8.1 Beta and you will see the same warning, but you will also get the value. "
使用 Xcode 8.1 Beta 构建,您将看到相同的警告,但您也将获得该值。”
reference : https://forums.developer.apple.com/thread/51348
回答by Lee Probert
I was getting a similar error:
我收到了类似的错误:
[User Defaults] attempt to set <private> for key in <private> in read-only (due to a previous failed write) preferences domain CFPrefsPlistSource<0x1700f5f80>
but it only happened when I was plugged into Xcode.
但它只发生在我插入 Xcode 时。
A device reboot fixed this.
设备重启解决了这个问题。
回答by Milander
I just had the same error. The error for me was because I called synchronize multiple times.
我只是有同样的错误。我的错误是因为我多次调用同步。
By calling [userDefaults synchronize] just once all the error messages disappeared.
通过调用 [userDefaults synchronize] 一次,所有错误消息都消失了。
回答by H u Y an g
if you suffer this problem when you try to save data to extension APP by using userDefault,maybe you had written this code : [[NSUserDefaults standardUserDefaults] initWithSuiteName:@"group.xxx.com"];,this code reset default userDefault. Actually,the correct code is : [[NSUserDefaults alloc] initWithSuiteName:@"group.xxx.com"]; http://www.jianshu.com/p/e782104c3bc3
如果您在尝试使用 userDefault 将数据保存到扩展 APP 时遇到此问题,也许您编写了此代码:[[NSUserDefaults standardUserDefaults] initWithSuiteName:@"group.xxx.com"];,此代码重置默认 userDefault。实际上,正确的代码是: [[NSUserDefaults alloc] initWithSuiteName:@"group.xxx.com"]; http://www.jianshu.com/p/e782104c3bc3
回答by sigrem
I just deleted the app and installed again via Xcode. Using XCode 8.2.1.
我刚刚删除了该应用程序并通过 Xcode 重新安装。使用 XCode 8.2.1。
回答by GeneCode
I'd like to share my solution for this problem. For my case, the error was a legitimate error and my data was not saved properly. It turns out the error was caused by having a NSUserDefaults "data saving" in a loop.
我想分享我对这个问题的解决方案。就我而言,该错误是一个合法的错误,我的数据没有正确保存。结果证明该错误是由循环中的 NSUserDefaults“数据保存”引起的。
By "data saving" I mean this (pseudocode):
通过“数据保存”我的意思是这个(伪代码):
loop{
[defaults setObject:@"Data" forKey:@"Key"];
[defaults synchronize];
}
So the code executes this multiple times very fast and cause the problem. I guess "synchronize" cannot handle concurrent calls.
所以代码非常快速地多次执行并导致问题。我猜“同步”无法处理并发调用。
What needs to be done is:
需要做的是:
loop{
[defaults setObject:@"Data" forKey:@"Key"];
}
[defaults synchronize];
synchronize be called once after all the objects been set.
在设置所有对象后调用一次同步。