简体   繁体   中英

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'

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM