Nirvana Tikku
Nirvana Tikku

Reputation: 4205

Access Titles and Values from a MultiValueSpecifier preference in Settings.bundle

I've got the following setup in my Root.plist file as part of my app's Settings.bundle:

<dict>
    <key>PreferenceSpecifiers</key>
    <array>
        <dict>
            <key>Type</key>
            <string>PSMultiValueSpecifier</string>
            <key>Title</key>
            <string>Title</string>
            <key>Key</key>
            <string>my_key</string>
            <key>DefaultValue</key>
            <string>default_value</string>
            <key>Titles</key>
            <array>
                <string>Default Value</string>
                <string>First</string>
                <string>Second</string>
                <string>Third</string>
                <string>Fourth</string>
            </array>
            <key>Values</key>
            <array>
                <string>default_value</string>
                <string>one</string>
                <string>two</string>
                <string>three</string>
                <string>four</string>
            </array>
        </dict>
    </array>
    <key>StringsTable</key>
    <string>Root</string>
</dict>

I would like to get access to the Titles and Values so that I can effectively determine the Title (i.e. the string representation) of a particular preference, and assign a text view's text to that value.

For example,

*defaultValue = [NSUserDefaults valueForKey:@"my_key"]; 
// let's say it's 'one'
textView setText:[[self getTitleForValue:defaultValue]]; 
// i want getTitleForValue to return 'First'

Do I have to parse the Root.plist file myself? and then build/reconcile the mapping between Titles and Values manually?

The settings list is long enough that I would prefer not to have to write an if statement after doing a lookup in NSUserDefaults, so I definitely want to find a solution that will allow me to look it up.

Upvotes: 1

Views: 619

Answers (2)

Nirvana Tikku
Nirvana Tikku

Reputation: 4205

For those interested, This is what I ended up with

NSString *bPath = [[NSBundle mainBundle] bundlePath];
NSString *settingsPath = [bPath stringByAppendingPathComponent:@"Settings.bundle"];
NSString *plistFile = [settingsPath stringByAppendingPathComponent:@"Root.plist"];
NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFile];
NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"];
NSArray *titles = [[preferencesArray objectAtIndex:0] objectForKey:@"Titles"];
NSArray *values = [[preferencesArray objectAtIndex:0] objectForKey:@"Values"];
int index = [values indexOfObject:currentValue];
if( index > -1 ){
    return [titles objectAtIndex:index];
}
return @"Default Title";

Upvotes: 0

iPatel
iPatel

Reputation: 47069

For Title:

NSLog(@"%@",[[[[yourdic objectForKey:@"PreferenceSpecifiers"] objectAtindex:0] objectForKey:@"Titles"] objectAtIndex:0]); 

//OR

NSLog(@"%@",[[[yourdic objectForKey:@"PreferenceSpecifiers"] objectAtindex:0] objectForKey:@"Titles"]); 

For Value:

NSLog(@"%@",[[[[yourdic objectForKey:@"PreferenceSpecifiers"] objectAtindex:0] objectForKey:@"Values"] objectAtIndex:0]);

//OR

NSLog(@"%@",[[[yourdic objectForKey:@"PreferenceSpecifiers"] objectAtindex:0] objectForKey:@"Values"]);

Upvotes: 1

Related Questions