0

我打算在选取器视图中的 settings.bundle 中使用 PSMultiValueSpecifier 中的标题。我只知道如何根据 settings.bundle 中的键获取我选择的值。是否可以根据密钥获得所有标题?

4

2 回答 2

1

(可能有更优雅的方式)。您可以获取设置字典并浏览其内容(前提是您知道其内部结构):

NSString* listPath = [[[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"] stringByAppendingPathComponent:@"Root.plist"];
NSDictionary* ttDict = [NSDictionary dictionaryWithContentsOfFile:listPath];
if (ttDict != nil){
    NSArray* prefsArray = [ttDict objectForKey:@"PreferenceSpecifiers"];
    // Next you get dictionary for required setting by its index (you should know it)
    NSDictionary* settingDict = [prefsArray objectAtIndex: index];
    NSArray* titlesArray = [settingDict objectForKey:@"Titles"]; // Voila
}

请注意,您以这种方式获得的标题未本地化。

于 2010-01-13T09:04:47.470 回答
1

这是我的版本,它还使用说明符 KEY 来定位 PSMultiValueSpecifier 而不记住它位于哪个索引处。索引可能会随着时间而变化(由于设置包的变化)。

NSString* settingsBundle = [[[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"] stringByAppendingPathComponent:@"Root.plist"];

NSDictionary* rootPlist = [NSDictionary dictionaryWithContentsOfFile:settingsBundle];

if (rootPlist == nil)
    return nil;

NSArray* specifiers = [rootPlist objectForKey:@"PreferenceSpecifiers"];

NSDictionary *multiValueSpecifier = nil;

for (NSDictionary *specifier in specifiers)
{
    if ([[specifier objectForKey:@"Key"] isEqualToString:speficierKey] == YES &&
        [[specifier objectForKey:@"Type"] isEqualToString:@"PSMultiValueSpecifier"] == YES)
    {
        multiValueSpecifier = specifier;
        break;
    }
}

if (multiValueSpecifier == nil)
    return nil;

NSArray* titlesArray = [multiValueSpecifier objectForKey:@"Titles"];
NSArray* valuesArray = [multiValueSpecifier objectForKey:@"Values"];

NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:titlesArray 
                                                       forKeys:valuesArray];

return dictionary;

于 2010-11-10T09:06:51.023 回答