iPhone:如何使用键从 NSMutable 数组中获取值
我想知道如何使用键从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题类型的字典似乎只包含一个键,因此您可以实现您想要的,但请记住:字典是使用键的无序集合,而数组是有序使用索引的集合。
无论如何,在
tableView:cellForRowAtIndexPath:
中,修改代码以获取字典中唯一的键(即问题 id):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 :事实上,我真的不明白你为什么使用字典数组。
您可以简单地构建问题文本数组:
但是如果您需要 qtype 用于其他目的,您可以获得
In the fact, I really don't understand why you use an array of dictionaries.
You can simply build array of question texts:
But if you need qtype for other purposes, you can get
这是因为你将 NSDictionary 存储在“qtype”中。尝试:
It is because you store NSDictionary in "qtype". Try:
您可以访问
NSDictionary
类型的数组的每个对象。以下是其代码you can access each object of array which is of
NSDictionary
type.Following is the code for that