1

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>

我想访问TitlesandValues以便我可以有效地确定Title特定偏好的(即字符串表示),并将文本视图的文本分配给该值。

例如,

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

我必须自己解析Root.plist文件吗?然后手动构建/协调标题和值之间的映射?

设置列表足够长,我不希望在查找后不必编写 if 语句NSUserDefaults,所以我肯定想找到一个允许我查找它的解决方案。

4

2 回答 2

1

对于标题:

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

//或者

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

对于价值:

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

//或者

NSLog(@"%@",[[[yourdic objectForKey:@"PreferenceSpecifiers"] objectAtindex:0] objectForKey:@"Values"]);
于 2013-02-28T08:40:06.157 回答
0

对于那些感兴趣的人,这就是我最终得到的

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";
于 2013-03-01T00:31:36.907 回答