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

发布于 2024-12-24 16:58:04 字数 743 浏览 0 评论 0原文

我想知道如何使用键从 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”。 如果您能给我一些想法,我将不胜感激。

I would like to know how to fetch a value from a NSMutableArray with keys. The array is built like this:

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

} 

I am populating the array in a table cell:

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

}

however the output is like "65 = YN" which is not what i want. I would like to extract only "65".
If you could give me some idea, would be very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱桃奶球 2024-12-31 16:58:05

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

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

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

It seems the dictionary for the question type contains only one key, so you can achieve what you want, but remember : dictionaries are unordered collections working with keys, whereas arrays are ordered collections working with indexes.

Anyway, in tableView:cellForRowAtIndexPath:, modify the code to get the only key (which is the question id) in the dictionary :

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *dictKeys = [[qtype objectAtIndex:[indexPath row]] allKeys];
    NSString *type = [dictKeys objectAtIndex:0];
}
人生百味 2024-12-31 16:58:05

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

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

}

In the fact, I really don't understand why you use an array of dictionaries.
You can simply build array of question texts:

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

}

But if you need qtype for other purposes, you can get

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

}
请你别敷衍 2024-12-31 16:58:05

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

NSDictionary *dict = [qtype objectAtIndex:[indexPath row]];
NSString *type = [[dict allValues] lastObject];//since you have only one object stored.

It is because you store NSDictionary in "qtype". Try:

NSDictionary *dict = [qtype objectAtIndex:[indexPath row]];
NSString *type = [[dict allValues] lastObject];//since you have only one object stored.
新一帅帅 2024-12-31 16:58:05

您可以访问 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"];
}

you can access each object of array which is of NSDictionary type.Following is the code for that

-(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"];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文