OSX NSUserDefaults不起作用 [英] OSX NSUserDefaults not Working

查看:115
本文介绍了OSX NSUserDefaults不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在我的应用程序中始终使我无法使用.它确实可以在我复制它的任何其他项目中工作...所以必须将我的standardUserDefaults弄乱了,但是我绝对不知道这怎么可能发生以及如何解决!

This code gives me always NO in my application. It does indeed work in any other project I copy it... so something must be messed up with my standardUserDefaults, but I absolutely don't know how this can happen and how to solve it!

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"myKey"];

BOOL test = [defaults boolForKey:@"myKey"];
// test is ALWAYS NO here!

任何人都可以提示我,从哪里开始或如何摆脱它?

Can anybody hint me, where to start or how to get rid of this?

这是一个包含Swift和目标C的混合项目,当我直接将它放入我的applicationDidFinishLaunching

It's a mixed project with swift and objective c and get the same behavior in me AppDelegate.swift, when I put this directly in my applicationDidFinishLaunching

let defaults = NSUserDefaults.standardUserDefaults()        
defaults.setBool(true, forKey: "myKey")

let test = defaults.boolForKey("myKey")
// test ALWAYS false here   

在有人问: -是的,即使之间调用了同步 -是的,这是完整的代码,两行之间没有任何内容...设置并正确处理后不起作用

Before someone asks: - yes, even with synchronize called between it - yes this is the whole code, nothing between the lines... set and get it right after does not work

推荐答案

我找到了一个解决方案,如何解决它,但为什么不这样呢?到目前为止,还没有干净,好的方法.

I found a solution, how to fix it, but not why this happens and no clean, good way so far.

如果有人可以解释一下并给我更好的解决方案,那就这样做!

If anybody could explain this and give me a better solution, do so!

  1. 问题是: 如果删除沙盒应用程序的容器,则还将删除NSUserDefaults的plist,并且不会再次创建它,因此NSUserDefaults根本无法正常工作.

  1. the problem was: If you delete the container of a sandboxed app, you also delete the plist for the NSUserDefaults and it's not created again and so NSUserDefaults simply is not working.

解决方法: 我在这里找到 https://ind.ie/labs/blog/app-sandbox-updating-nsuserdefaults-fails-after-deleting-apps-container-directory/,这是系统永久书签的问题. 好了,只是清空垃圾桶对我来说不起作用,但是起作用了:我只是创建了丢失的文件!

the workaround: As I found here https://ind.ie/labs/blog/app-sandbox-updating-nsuserdefaults-fails-after-deleting-apps-container-directory/ it's a problem with the permanent bookmark of the system. Well just to empty the trash does not work for me, but what worked: I simply created the missing file!

touch ~/Library/Containers/com.example.myapp/Data/Library/Preferences/com.example.myapp.plist

这篇关于OSX NSUserDefaults不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆