27
votes

I have a multi value setting (location) defined in my settings bundle for an app.
The Titles are defined as long titles, for example, "London" and the corresponding value part of the setting is defined as "1".

[EDIT]

To expand this question I will add more information about the multi value setting:

this is a screenshot of the multi value setting

How do I retrieve the Title of LONDON at Item 0. As described above when I retrieve the objectForKey or valueForKey I get 1 always. I want to display the string "LONDON" from the title in a label in the app but use the value 1 in core data. Note: I have set the value in the settings before the app runs so it does return a value but the value is always 1 as the Title does not seem to be accessible.

4
I know how to get the settings. I want to know how to display the Title which is different from the value in my example above. When I use your example above objectForKey:@"locSetting" returns LDN. When I use valueForKey:@"locSetting" I also get LDN returned. But I have the Title in my multi value settings in the settings.bundle set to "London".motionpotion
@H2CO3 Google is not down. Your answer doesn't answer my specific scenario. But thanks for responding.motionpotion
Default value is not about index position, but valuesjomafer

4 Answers

12
votes

You should set your "Default Value" value as "1" - Just Item 0 from your "Values" list.

6
votes

I ran into the similar situation and answering the question that it'll help someone.

If I understand the question correctly, when user selects London from the settings the default value will be 1. when you read the settings bundle for locationSetting you get the value 1 and not LONDON, but you want the "LONDON" in your code rather than 1. It is not possible with the current structure of your plist. make the default value a dictionary instead of string and then you can have both titles and values inside that dictionary. you can then retrieve the text of the selected value.

2
votes

Location settings as a multi value, is an NSDictionary. Therefore, do the following:

NSDictionary *location = [[NSUserDefaults standardUserDefaults] valueForKey:@"locationSettings"]; 

NSString *value = (NSString *)location; 

Cast the location value as a NSString because you know a string is to be returned.

You can then handle the result as a string. The "value" variable returned is whatever is written in the value section. In your case, if you select "London", you will get a value of "1" back in the form of a string.

0
votes

If my understanding of your question is correct, you are asking how you can get the Key entry of the location dictionary for the user's chosen value.

For dictionaries, there is no such single-use method. But there is a way to achieve the same result via manipulating allKeys as below:

NSDictionary *location = [[NSUserDefaults standardUserDefaults] valueForKey:@"locationSettings"]; 
NSArray *keys = [location allKeys];
NSString *locationIdentifier = [keys objectForIndex:[location valueForKey:@"LONDON"]];

This would result in the "LONDON" string being stored in the locationIdentifier variable. So that last line, the assignment of the string, could possibly be changed to simply have objectForIndex:0 instead, but that isn't something I've tried before so I'm not certain.