开发者

How do you create and access array elements in settings bundle

开发者 https://www.devze.com 2023-03-04 05:10 出处:网络
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 o

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 anywhe开发者_开发技巧re. In xcode, I have created the Root.plist and it looks something like:

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 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.

Can someone help me with this. Also, what would be the correct way to update those values?


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 after the 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.

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"];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号