xcode 如何在设置包中创建和访问数组元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5923373/
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
How do you create and access array elements in settings bundle
提问by JeffB6688
I want to save strings as array elements in a Settings.bundle Root.plist and be able to access those strings from the Setting bundle on subsequent launches of my app. I could not find a good example of this anywhere. In xcode, I have created the Root.plist and it looks something like:
我想将字符串保存为 Settings.bundle Root.plist 中的数组元素,并能够在我的应用程序后续启动时从设置包中访问这些字符串。我在任何地方都找不到这样的好例子。在 xcode 中,我创建了 Root.plist,它看起来像:
Key
iPhone Setting Schema
Strings Filename
Preference Items
Item 0 (Title- My Title)
Type Title
Title My Title
Identifier My Identifier
Values
Item 0 my string 1
Item 1 my string 2
Item 3 my string 3
This produces xml as follows:
这将生成如下 xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSTitleValueSpecifier</string>
<key>Title</key>
<string>My Title</string>
<key>Key</key>
<string>My Identifier</string>
<key>Values</key>
<array>
<string>my string 1</string>
<string>my string 2</string>
<string>my string 3</string>
</array>
</dict>
I am using the following code to attempt an access of the Values from the Identifier key "My Identifier":
我正在使用以下代码尝试从标识符键“我的标识符”访问值:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *mystrings = [defaults arrayForKey:@"My Identifier"];
for (int i=0;i<3;i++) {
NSLog(@"%@", [mystrings objectAtIndex:i]);
}
The value of mystrings is 0 at runtime. Consequently, the Values of "my string 1", "my string 2", and "my string 3" are not getting printed by the NSLog.
mystrings 的值在运行时为 0。因此,NSLog 不会打印“我的字符串 1”、“我的字符串 2”和“我的字符串 3”的值。
Can someone help me with this. Also, what would be the correct way to update those values?
有人可以帮我弄这个吗。另外,更新这些值的正确方法是什么?
采纳答案by jscs
Part of your problem is mentioned under the heading "Specifying Default Values for Preferences":
在标题“为首选项指定默认值”下提到了您的部分问题:
default preference values from the application's Settings bundle are not set until the Settings application runs. This means that if the user runs your application before running Settings, the default values specified in your Settings bundle are unavailable.
在设置应用程序运行之前,不会设置应用程序设置包中的默认首选项值。这意味着如果用户在运行设置之前运行您的应用程序,则设置包中指定的默认值将不可用。
All the stuff in the Settings bundle doesn't get loaded for your app's defaults database until afterthe user opens the Settings Application. Does this seem stupid to you? Seems stupid to me. The result is that you need to register all those defaults initially yourself. I wrote this while I was trying to figure out what was going on; improvements can definitely be made, but it should get you started:
在设置捆绑所有的东西中没有加载你的应用程序的默认数据库,直到后用户打开设置应用程序。你觉得这很愚蠢吗?对我来说似乎很愚蠢。结果是您最初需要自己注册所有这些默认值。我在试图弄清楚发生了什么时写了这篇文章;肯定可以进行改进,但它应该让你开始:
NSURL * settingsURL = [[NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"Settings" withExtension:@"bundle"]]
URLForResource:@"Root" withExtension:@"plist"];
NSDictionary * settingsDict = [NSDictionary dictionaryWithContentsOfURL:settingsURL];
NSArray * settingsArr = [settingsDict objectForKey:@"PreferenceSpecifiers"];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
for( NSDictionary * setting in settingsArr ){
NSString * key = [setting objectForKey:@"Key"];
if( !key ) continue; // Some objects don't have keys
if( ![defaults objectForKey:key] ){
[defaults setObject:[setting objectForKey:@"DefaultValue"]
forKey:key];
}
}
Another problem that you have is that you don't get an array for PSTitleValueSpecifier
keys when you do [defaults objectForKey:@"myTitlePreferencesKey"]
; you just get one string. A @"DefaultValue"
key/value pair is also required to be part of that item's dictionary. Initially you will get that value, which must be a string. If you change the value, you'll get the string from the Titles
array which has the same index as that object in the Values
array. I'm pretty sure that if you have a Values
array, you also have to have a Titles
array.
你遇到的另一个问题是,PSTitleValueSpecifier
当你这样做时,你没有得到一个键数组[defaults objectForKey:@"myTitlePreferencesKey"]
;你只得到一根绳子。@"DefaultValue"
还需要一个键/值对作为该项目的字典的一部分。最初您将获得该值,该值必须是一个字符串。如果更改该值,则会从Titles
数组中获取与数组中该对象具有相同索引的字符串Values
。我很确定如果你有一个Values
数组,你也必须有一个Titles
数组。
For the second part of your question, if you want to change the values of any of the preferences, you simply do
对于问题的第二部分,如果您想更改任何首选项的值,您只需执行
[[NSUserDefaults standardUserDefaults] setObject:myValue
forKey:@"myKey"];