Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone:如何使用键从NSMutable数组中获取值_Iphone_Objective C_Nsdictionary - Fatal编程技术网

iPhone:如何使用键从NSMutable数组中获取值

iPhone:如何使用键从NSMutable数组中获取值,iphone,objective-c,nsdictionary,Iphone,Objective C,Nsdictionary,我想知道如何从带有键的NSMutableArray中获取值。阵列的构建方式如下: qtype = [[NSMutableArray alloc] init]; while (dicjson = (NSDictionary*)[enumerator nextObject]) { question_id = [dicjson objectForKey:@"question_id"]; question_text = [dicjson objectF

我想知道如何从带有键的NSMutableArray中获取值。阵列的构建方式如下:

    qtype = [[NSMutableArray alloc] init];
    while (dicjson = (NSDictionary*)[enumerator nextObject]) {
     question_id = [dicjson objectForKey:@"question_id"];        
     question_text = [dicjson objectForKey:@"question_text"];
     question_type = [dicjson objectForKey:@"question_type"];

     [qtype addObject:[NSDictionary dictionaryWithObject:question_type forKey:question_id]];

} 
我正在表格单元格中填充数组:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *type = [qtype objectAtIndex:[indexPath row]];

}
然而,输出像“65=YN”,这不是我想要的。我只想摘录“65”。
如果您能给我一些建议,我将不胜感激。

这是因为您将NSDictionary存储在“qtype”中。尝试:


问题类型的字典似乎只包含一个键,因此您可以实现所需的功能,但请记住:字典是使用键的无序集合,而数组是使用索引的有序集合

无论如何,在
tableView:cellforrowatinexpath:
中,修改代码以获取字典中的唯一键(即问题id):

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *dictKeys = [[qtype objectAtIndex:[indexPath row]] allKeys];
    NSString *type = [dictKeys objectAtIndex:0];
}

事实上,我真的不明白你为什么要使用一系列字典。
您可以简单地构建问题文本数组:

qtype = [[NSMutableArray alloc] init];
while (dicjson = (NSDictionary*)[enumerator nextObject]) {
 question_id = [dicjson objectForKey:@"question_id"];        
 question_text = [dicjson objectForKey:@"question_text"];
 question_type = [dicjson objectForKey:@"question_type"];

 [qtype addObject:question_id];

 } 

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *type = [qtype objectAtIndex:[indexPath row]];

}
但如果出于其他目的需要qtype,可以

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = [qtype objectAtIndex:[indexPath row]];
    NSString *type = [[dict allKeys] objectAtIndex:0]; 

}

您可以访问
NSDictionary
类型的数组的每个对象

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = [qtype objectAtIndex:[indexPath row]];
    NSString *question_id = [dict objectForKey:@"question_id"];     
    NSString *question_text = [dict objectForKey:@"question_text"];
    NSString *question_type = [dict objectForKey:@"question_type"];
}

谢谢,对我有用。我需要这样做,因为这是从服务器读取json代码。我相信在NSMutableArray上这是不可能的。NSMutableDictionary可以做到这一点。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = [qtype objectAtIndex:[indexPath row]];
    NSString *question_id = [dict objectForKey:@"question_id"];     
    NSString *question_text = [dict objectForKey:@"question_text"];
    NSString *question_type = [dict objectForKey:@"question_type"];
}