Iphone 数组无法获取特定键的数据
像这样的响应数组Iphone 数组无法获取特定键的数据,iphone,ios,objective-c,Iphone,Ios,Objective C,像这样的响应数组 NewDataSet = { Table = ( { City = { text = "\nThiruvananthapuram"; }; Country = { te
NewDataSet = {
Table = (
{
City = {
text = "\nThiruvananthapuram";
};
Country = {
text = "\n\nIndia";
};
text = "\n";
},
{
City = {
text = "\nVellore";
};
Country = {
text = "\n\nIndia";
};
text = "\n";
}
);
text = "\n";
我写了这段代码
xmlDictionary = [XMLReader dictionaryForXMLString:xmlResultString error:nil];
NSLog(@"%@",xmlDictionary);
NSLog(@"%@",xmlDictionary);
NSArray * responseArr = xmlDictionary[@"NewDataSet"];
NSLog(@"%@",responseArr);
for(NSDictionary * dic in responseArr)
{
NSLog(@"%@",dic);
//[array1 addObject:[dic valueForKey:@"City"]];
[array1 addObject:[[dic valueForKey:@"City"] valueForKey:@"text"]];
}
但是没有得到数据。排成一列。请先帮我解决这个问题,谢谢
问题是我无法在NSDictionary中获取值。
错误日志为
此类不符合关键城市的键值编码。您似乎没有在任何地方分配阵列1 您的解决方案如下:
array1 = [[NSMutableArray alloc]init];
for(NSDictionary * dic in responseArr)
{
[array1 addObject:[dic valueForKey:@"City"]];
}
NSLog(@"array1 >> %@",array1);
如果您已经分配了阵列1,请尝试记录[dic valueForKey:@“City”]
的值
试试这个试试这个代码
for(int i =0;i<[responseArr count];i++)
{
NSString *str = [[responseArr objectAtIndex:i] valueForKey:@"City"];
[array1 addObject:str];
}
for(int i=0;i我得到了我的解决方案问题是我不会得到直接的城市密钥。因为城市密钥在NewDataSet&Table下
因此,首先转到NewDataSet,然后是表键,最后是城市键
现在从字典中获取数组数据
NSArray * City=[[NSArray alloc]init];
City=[[[xmlDictionary valueForKey:@"NewDataSet"] valueForKey:@"Table"] valueForKey:@"City"];
NSLog(@"%@",City);
在键内传递多个键这是解决方案。您是否在Responsearray中获取数据?是的,在Responsearray中获取数据..只需打印该数据..显示该数据..您是否在任何位置分配了阵列1..分配并初始化它很重要?是的,我已经完成了这是viewDidLoad()…开始描述答案,以便更好地为他人实践和学习。发生的事情iddic
不包含“城市”key?@jitendradore:你能打印出你的数据的准确日志吗?是的,我知道这不是完整的解决方案。但我会一步一步地指导你。所以请让我知道你在dic中得到了值吗?如果是,什么值?会发生什么情况iddic
不包含“城市”密钥?-[\uu nsDictionary objectAtIndex:]:无法识别的选择器发送到实例0xfe61d90'当我尝试您的代码时出现此错误。.您没有显示正确的数据…缺少一些括号。请使用所有括号显示正确的数据。。。
NSArray * City=[[NSArray alloc]init];
City=[[[xmlDictionary valueForKey:@"NewDataSet"] valueForKey:@"Table"] valueForKey:@"City"];
NSLog(@"%@",City);