简体   繁体   English

NSInvalidArgumentException”,原因:“-[__ NSCFConstantString objectForKey:]:无法识别的选择器已发送到实例0x10256a1b0”

[英]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.

相关问题 'NSInvalidArgumentException',原因:'-[__ NSCFConstantString addObject:]:无法识别的选择器已发送到实例 - 'NSInvalidArgumentException', reason: '-[__NSCFConstantString addObject:]: unrecognized selector sent to instance __NSCFConstantString objectForKey无法识别的选择器发送到实例错误 - __NSCFConstantString objectForKey unrecognized selector sent to instance error 终止应用程序-'NSInvalidArgumentException',原因:'-[NSCFString objectForKey:]:无法识别的选择器已发送到实例 - Terminating app - 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 解析json并获取异常,原因:'-[__ NSCFArray objectForKey:]:无法识别的选择器已发送到实例0x7b1c7630 - parsing json and getting exception, reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7b1c7630 'NSInvalidArgumentException',原因:'-[UIImage length]:无法识别的选择器发送到实例0x1d53b6b0' - 'NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0x1d53b6b0' 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__ NSArrayM objectForKey:]:无法识别的选择器已发送至实例 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 'NSInvalidArgumentException',原因:'-[NSTaggedPointerString objectForKey:]:无法识别的选择器已发送到实例0xa000000617461644' - 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0xa000000617461644' 解析json并获取NSInvalidArgumentException',原因:'-[__ NSCFArray objectForKey:]:无法识别的选择器已发送到实例 - parsing json and getting NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance NSInvalidArgumentException原因:无法识别的选择器发送到实例 - NSInvalidArgumentException reason : unrecognized selector sent to instance NSInvalidArgumentException,原因无法识别的选择器发送到实例 - NSInvalidArgumentException, reason unrecognized selector sent to instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM