在程序上等同于“默认写入".命令,例如,如何使用NSUserDefaults? [英] Programmatic Equivalent of "defaults write" command, e.g., how to use NSUserDefaults?

查看:133
本文介绍了在程序上等同于“默认写入".命令,例如,如何使用NSUserDefaults?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以编程方式尝试执行OS X中的默认写入"命令.我似乎无法弄清楚如何为要查找的域获取正确的首选项字典.我可以在下面的代码中获得某些域的首选项,但是有问题的首选项似乎不在字典中.

Trying programmatically do what the 'defaults write' command does in OS X. I can't seem to figure out how to get the correct preferences dictionary for the domain I'm looking for. I can get some preferences for the domains in the code below, but the preferences in question don't seem to be in the dict.

为什么/如何在终端命令中而不是在代码中?它们不是标准的用户默认设置吗?只是似乎找不到它们.

Why/How are they in the terminal command but not in the code? Are they not in the standard user defaults? Just can't seem to find them.

这些是我要输入到代码中的命令:

these are the commands I'm trying to put into code:

defaults write com.apple.dock mcx-expose-disabled -bool true
defaults write com.apple.dashboard mcx-disabled -bool true




NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSMutableDictionary   *dockDict = [[defaults persistentDomainForName:@"com.apple.dock"] mutableCopy];
NSMutableDictionary   *dashDict = [[defaults persistentDomainForName:@"com.apple.dashboard"] mutableCopy];

[dockDict setValue:YES forKey:@"mcx-expose-disabled"];


[defaults setPersistentDomain:dockDict forName:@"com.apple.dock"];
[defaults setPersistentDomain:dashDict forName:@"com.apple.dashboard"];

推荐答案

唯一的问题是您所在的行:

The only problem is your line here:

 [dockDict setValue:YES forKey:@"mcx-expose-disabled"];

应该是

 [dockDict setValue:[NSNumber numberWithBool:YES] forKey:@"mcx-expose-disabled"];

Objective-C不会将原始类型的值自动装箱"到对象中.

Objective-C doesn't "auto-box" values of primitive types into objects.

而且,编译器应已警告您说您不能将YES传递给setValue:forKey:. 您应该检查编译器发出的每条警告!这就是警告的目的!

And, the compiler should have given you a warning saying that you can't pass YES to setValue:forKey:. You should inspect every warning the compiler emits! That's what the warnings are for!

这篇关于在程序上等同于“默认写入".命令,例如,如何使用NSUserDefaults?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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