Search code examples
iphoneiosobjective-cios6.1

i want to this string array arrange in key value form like hashmap


("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.


Solution

  • 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]];
    }