我有一个视图控制器,我在其中解析xml文件并在字典中获取其值,我试图获取标签中的值,但我的应用程序因显示此错误而崩溃{{ 1}}
我从字典中获取值的代码就是这些,
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x10256a1b0'
如何删除此错误? 这是我的字典,
NSLog(@"string: %@", str);
NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:str];
NSLog(@"dictionary: %@", xmlDoc);
for (NSDictionary * oneCustomer in xmlDoc)
{
// Create a new Customer record
_lblID.text = [oneCustomer objectForKey:@"_id"];
NSLog(@"Reg: %@ ",_lblID.text);
_lblAlert.text = [oneCustomer objectForKey:@"_topic"];
NSLog(@"Make: %@ ",_lblAlert.text);
_lblxref.text = [oneCustomer objectForKey:@"xref"];
NSLog(@"Type: %@ ",_lblxref.text);
_lbltext.text = [oneCustomer objectForKey:@"text"];
NSLog(@"Model: %@ ", _lbltext.text);
_lblgraphic.text = [oneCustomer objectForKey:@"explanation"];
NSLog(@"Model: %@ ",_lblgraphic.text);
_lblprompt.text = [oneCustomer objectForKey:@"prompt"];
NSLog(@"Model: %@ ",_lblprompt.text);
_lblvoice.text = [oneCustomer objectForKey:@"_id"];
NSLog(@"Model: %@ ", _lblvoice.text);
//roils = [oneCustomer objectForKey:@"recommended oil"];
//NSLog(@"Model: %@ ",roils);
}
我必须从这个字典中提取所有值的值,我该如何得到它?
答案 0 :(得分:0)
我希望这可以帮到你,
-(void)fetchValuesFromDictioanry:(NSDictionary*)xmlDict
{
for (NSString *string in [xmlDict allKeys])
{
if ([[xmlDict valueForKey:string] isKindOfClass:[NSDictionary class]])
{
[self fetchValuesFromDictioanry:[xmlDict valueForKey:string]];
}
else if ([[xmlDict valueForKey:string] isKindOfClass:[NSString class]])
{
if ([string isEqualToString:@"_id"] && [[xmlDict valueForKey:string] isKindOfClass:[NSString class]])
{
_lblID.text = [xmlDict valueForKey:@"_id"];
}
else
{
NSLog(@"%@", NSStringFromClass([[xmlDict valueForKey:string] class]));
}
NSLog(@"Model: %@ ", [xmlDict valueForKey:string]);
//Set here other labels
}
else if ([[xmlDict valueForKey:string] isKindOfClass:[NSArray class]])
{
for (NSDictionary *dictionary in [xmlDict valueForKey:string]) {
[self fetchValuesFromDictioanry:dictionary];
}
}
}
}
你可以这样打电话,
[self fetchValuesFromDictioanry:xmlDoc];