-1

("1$Indore","2$Lyallpur","3$Sagan","4$Bhopal","5$Reva","6$Santa","7$Dar","8$Chinaware","9$Gwalior","10$Jain","11$Morena","12$West Nimar","13$Chatterer")

I want to this string array arrange in key value form like hash map from the "$" and select value like Indore in UIPicker and result in its key like 1. pl z help me... but plz more justify because this string fetch from server i cant write self use in my code key is 1 and value is Indore.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80
  • m just giving you the idea logic is upto you:- you can use for loop and inside that separate the strings with $ and store two values into different arrays then you'll be able to have access on whatever value you want. – D-eptdeveloper Oct 15 '13 at 11:33
  • use NSDictionary for key value pairs – Atul Oct 15 '13 at 13:24

2 Answers2

0

A NSDictionary can be used as a hash map and the syntax is very simple. A NSDictionary literal is declared using @{ } and can contain a comma-seperated list of key-value pairs. These key-value pairs are declared as key: value.

For example:

NSDictionary *dict = @{ @"1": @"Indore", @"2":@"Lyallpur", ... };

EDIT: To convert the string array (let's call it values) in the original question to a NSDictionary, you'd do this:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *value in values) {
    NSArray *keyValuePair = [value componentsSeparatedByString:@"$"];

    [dict setObject:keyValuePair[1] forKey:keyValuePair[0]];
}
neilco
  • 7,964
  • 2
  • 36
  • 41
0

Use this NSDictionary below:-

//Add how many elements you want to add on your array
    NSDictionary *yourDict=[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"$Indore", nil] forKeys:[NSArray arrayWithObjects:@"1", nil]];
    NSString *yourStr=[yourDict objectForKey:@"1"];
    NSLog(@"%@",yourStr);
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • thanks but plz more justify because this string from server i cant self use in my code – Abhishek Maheshwari Oct 15 '13 at 11:54
  • oh ok then follow this link here you will get idea how to split string from server. Please follow this http://stackoverflow.com/questions/19364787/word-output-based-on-nsstring-sequences/19365935#19365935 – Hussain Shabbir Oct 15 '13 at 11:57