简体   繁体   English

从Settings.bundle中的MultiValueSpecifier首选项访问标题和值

[英]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 : 我的Root.plist文件中包含以下设置,作为应用程序的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 (ie the string representation) of a particular preference, and assign a text view's text to that value. 我想访问“ TitlesValues以便我可以有效地确定特定首选项的“ Title (即字符串表示形式),并为该值分配文本视图的文本。

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? 我是否必须自己解析Root.plist文件? 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. 设置列表足够长,以至于我不希望在NSUserDefaults进行查找后不必编写if语句,因此,我当然想找到一个允许我查找它的解决方案。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM