0

检索位于 Root.strings 中存储的 Root.plist 值的标题的最佳方法是什么?例如,我存储了一个 INT = 1,它的键为 MUL_VAL_2,对应于 Root.strings 中的“女性声音”。如何检索存储的 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>

4

2 回答 2

2

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

[settingsViewController.settingsReader titleForStringId:@"MUL_VAL_1"]
于 2014-03-31T10:34:56.490 回答
1

我认为最好的方法是将plist 加载为 NSDictionaryNSLocalizedStringFromTable ,检索 INT 值的“标题”键,然后通过宏从 Root.strings 获取该键的标题。

另请参阅: Apple 文档中的字符串资源

在这里如何做到这一点:

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

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

于 2014-03-30T04:31:19.130 回答