[英]NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x10256a1b0'
I have a view controller in which i'm parsing an xml file and getting its values in dictionary, i'm trying to get the values in the labels but my app is crashing by showing this error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x10256a1b0'
我有一个视图控制器,其中我正在解析一个xml文件并在字典中获取其值,我正在尝试获取标签中的值,但我的应用*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x10256a1b0'
显示此错误而崩溃*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x10256a1b0'
My code to get values from dictionary are these, 我从字典获取值的代码是这些,
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);
}
How to remove this error? 如何清除此错误? This is my dictionary , 这是我的字典,
dictionary: {
"__name" = QF;
"_category" = "C&M";
"_id" = AB2001;
"_ni_exempt" = no;
"_topic" = Alertness;
answers = {
answer = (
{
"_correct" = no;
text = "give an arm signal as well as using your indicators";
},
{
"_correct" = no;
text = "signal so that other drivers can slow down for you";
},
{
"_correct" = yes;
text = "look over your shoulder for a final check";
},
{
"_correct" = no;
text = "select a higher gear than normal";
}
);
};
question = {
explanation = {
text = "If you want to make a U-turn, slow down and ensure that the road is clear in both directions. Make sure that the road is wide enough to carry out the manoeuvre safely.";
voice = {
"_id" = "AB2001-2";
};
};
prompt = "Mark one answer";
text = "Before you make a U-turn in the road, you should";
voice = {
"_id" = "AB2001-1";
};
xref = "DES s4, DES s9, HC r159-161";
};
I have to extract the values from this dictionary all the values how can i get this? 我必须从这本字典中提取所有值,我该如何获取呢?
I hope this helps you, 我希望这可以帮助你,
-(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];
}
}
}
}
You can call like this, 你可以这样打
[self fetchValuesFromDictioanry:xmlDoc];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.