for(NSString *s in mainarr)
{
NSString newseparator = @"=";
NSArray *subarray = [s componentsSeparatedByString : newseparator];
//Copying the elements of array into key and object string variables
NSString *key = [subarray objectAtIndex:0];
NSLog(@"%@",key);
NSString *class_name= [subarray objectAtIndex:1];
NSLog(@"%@",class_name);
//Putting the key and objects values into hashtable
NSDictionary *dict= [NSDictionary dictinaryWithObject:@"class_name" forKey:@"key"];
}
Hello.. in the above code i ve to parse the elements of array in a for loop, and then have to put the substring key and class_name into a hashtable. how to put a value of those string variables into hashtable.
in the code above i guess the variables class_name and key are put into hashtable not the value. i suppose its a wrong method. wat can be done to achieve the solution?
(1), You should write
although directly using
is much better (or make
newseparatora global constant).(2), Your last statement,
is invalid because (a)
NSMutableDictionaryis a type; (b) you are creating a dictionary, not a mutable dictionary; (c) you are creating it every time, and overwriting the previous ones; (d) you are creating the dictionary with the constant values@"class_name"and keys@"key", which does not corresponds to the actual variablesclass_nameandkey.To add the key-value pairs into 1 hash table, you should create the mutable dictionary at the beginning
and then in the loop, use
-setObject:forKey:to add it into the dictionary:To conclude, you should modify the code as