user973984
user973984

Reputation: 2824

Retrieve the title of a stored plist value from localized .strings

What is the best method to retrieve the title for the stored Root.plist value located inside Root.strings ? For example I have stored an INT = 1 which has a key of MUL_VAL_2 that corresponds to "Female Voice" in Root.strings. How can I retrieve "Female Voice" for the stored 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>

Upvotes: 0

Views: 212

Answers (2)

Ortwin Gentz
Ortwin Gentz

Reputation: 54121

You can use IASK's own logic to retrieve the localized string:

[settingsViewController.settingsReader titleForStringId:@"MUL_VAL_1"]

Upvotes: 2

IvanRublev
IvanRublev

Reputation: 842

I think the best method is to load plist as NSDictionary, retrieve the 'Title' key for your INT value, then get title from Root.strings for that key via NSLocalizedStringFromTable macro.

See also: String resources in Apple's docs.

Here how to do this:

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

In the code above "your stored INT" is set in the storedInt variable. You can use, for example, for cycle to iterate over it's possible values.

Upvotes: 1

Related Questions