从本地化的.strings中检索存储的plist值的标题

时间:2014-03-30 03:10:01

标签: ios iphone objective-c inappsettingskit

检索Root.strings中存储的Root.plist值的标题的最佳方法是什么?例如,我存储了一个INT = 1,它具有MUL_VAL_2的键,对应于Root.strings中的“Female Voice”。如何检索存储的INT的“女声”?

 // Root.strings     
 "MUL_VALUE_TITLE"       = "Multiple Values";
 "MUL_VAL"               = "Sound Theme";
 "MUL_VAL_1"             = "Male Voice";
 "MUL_VAL_2"             = "Female Voice";
 "MUL_VAL_3"             = "Mechanical";


 // Root.plist
 <plist version="1.0">
 <dict>
<key>Titles</key>
<array>
    <string>MUL_VAL_1</string>
    <string>MUL_VAL_2</string>
    <string>MUL_VAL_3</string>
</array>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>Values</key>
<array>
    <integer>0</integer>
    <integer>1</integer>
    <integer>2</integer>
</array>
<key>Title</key>
<string>MUL_VAL</string>
<key>Key</key>
<string>timer_theme_mulValue</string>
<key>DefaultValue</key>
<integer>0</integer>
</dict>

2 个答案:

答案 0 :(得分:2)

您可以使用IASK自己的逻辑来检索本地化字符串:

[settingsViewController.settingsReader titleForStringId:@"MUL_VAL_1"]

答案 1 :(得分:1)

我认为最好的方法是load plist as NSDictionary,检索INT值的'Title'键,然后通过NSLocalizedStringFromTable宏从Root.strings获取该键的标题。

另请参阅:Apple的文档中的String resources

在这里如何做到这一点:

int storedInt = 1;
NSString * plistName = @"Root";
NSString * stringsTableName = @"Root";

// Loading plist
NSString * myPlistFilePath = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
NSDictionary * rootPlist = [NSDictionary dictionaryWithContentsOfFile:myPlistFilePath];

NSString * key = nil;
NSString * keyReadable = nil;

NSArray * values = rootPlist[@"Values"];
NSArray * titles = rootPlist[@"Titles"];
NSUInteger indexOfValue = [values indexOfObject:@(storedInt)];
if (indexOfValue != NSNotFound) {
    key = titles[indexOfValue];
    keyReadable = NSLocalizedStringFromTable(key, stringsTableName, @""); // Obtain readable title for key
} else {
    // handle 'undefined value in dictionary' error here.
}

NSLog(@"for value: %d key is: %@ and title of key is: %@", storedInt, key, keyReadable);

在上面的代码中,“您存储的INT”在storedInt变量中设置。例如,您可以使用循环来迭代它的可能值。