简体   繁体   English

如何在iPhone编码中将字符串值添加到哈希表中?

[英]How to add the value of string into hashtable in iPhone coding?

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. 您好..在上面的代码中,我必须在for循环中解析数组的元素,然后必须将子字符串键和class_name放入哈希表中。 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. 在上面的代码中,我想变量class_name和key放入哈希表中而不是值中。 i suppose its a wrong method. 我想这是一个错误的方法。 wat can be done to achieve the solution? 可以通过wat实现解决方案吗?

(1), You should write (1),你应该写

 NSString* newseparator = @"=";

although directly using 虽然直接使用

 NSArray *subarray = [s componentsSeparatedByString:@"="]; 

is much better (or make newseparator a global constant). 更好(或使newseparator成为全局常量)。


(2), Your last statement, (2),您的最后声明,

    NSMutableDictionary = [NSDictionary dictinaryWithObject:@"class_name" forKey:@"key"];

is invalid because (a) NSMutableDictionary is a type; 无效,因为(a) NSMutableDictionary是一种类型; (b) you are creating a dictionary, not a mutable dictionary; (b)您正在创建字典,而不是可变字典; (c) you are creating it every time, and overwriting the previous ones; (c)您每次都在创建它,并覆盖以前的内容; (d) you are creating the dictionary with the constant values @"class_name" and keys @"key" , which does not corresponds to the actual variables class_name and key . (d)您正在使用常量值@"class_name"和键@"key"创建字典,该常量值与实际变量class_namekey不对应。

To add the key-value pairs into 1 hash table, you should create the mutable dictionary at the beginning 要将键值对添加到1个哈希表中,应在开头创建可变字典

NSMutableDictionary* dict = [NSMutableDictionary dictionary];

and then in the loop, use -setObject:forKey: to add it into the dictionary: 然后在循环中,使用-setObject:forKey:将其添加到字典中:

[dict setObject:class_name forKey:key];

To conclude, you should modify the code as 最后,您应该将代码修改为

NSMutableDictionary* dict = [NSMutableDictionary dictionary];
for(NSString *s in mainarr) {
    NSArray *subarray = [s componentsSeparatedByString:@"="]; 

    // Get 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  
    [dict setObject:class_name forKey:key];
}    
return dict;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM