因未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSCFString objectForKey:]

时间:2013-05-03 08:36:04

标签: objective-c xml-parsing nsstring nsmutablearray nsdictionary

我的XML回复:

{
    "S:Envelope" =     {
        "S:Body" =         {
            "ns2:getMatchListResponse" =             {
                item =                 (
                                        {
                        answerTime =                         {
                            text = 30;
                        };
                        challengerAppealsGranted =                         {
                            text = "0 of 16";
                        };
                        challengerHandle =                         {
                            text = manish;
                        };
                        challengerMatchesPlayed =                         {
                            text = 93;
                        };
                        challengerPic =                         {
                            text = "image.png";
                        };
                        challengerScore =                         {
                            text = 0;
                        };
                        challengerlosses =                         {
                            text = 0;
                        };
                        challengerwins =                         {
                            text = 4;
                        };
                        handle1 =                         {
                            text = sahni;
                        };
                        handle1AppealsGranted =                         {
                            text = 5;
                        };
                        handle1MatchesPlayed =                         {
                            text = 84;
                        };
                        handle1Response =                         {
                            text = Pending;
                        };
                        handle1Score =                         {
                            text = 0;
                        };
                        handle1losses =                         {
                            text = 0;
                        };
                        handle1wins =                         {
                            text = 1;
                        };
                        handle2MatchesPlayed =                         {
                            text = 0;
                        };
                        handle2Score =                         {
                            text = 0;
                        };
                        handle2losses =                         {
                            text = 0;
                        };
                        handle2wins =                         {
                            text = 0;
                        };
                        handle3MatchesPlayed =                         {
                            text = 0;
                        };
                        handle3Score =                         {
                            text = 0;
                        };
                        handle3losses =                         {
                            text = 0;
                        };
                        handle3wins =                         {
                            text = 0;
                        };
                        idMatch =                         {
                            text = 750;
                        };
                        isAppeal =                         {
                            text = false;
                        };
                        isFreeForm =                         {
                            text = false;
                        };
                        isMultichoiceQuestion =                         {
                            text = false;
                        };
                        isPublic =                         {
                            text = false;
                        };
                        matchName =                         {
                            text = ewtwert;
                        };
                        matchStartThresholdTime =                         {
                            text = "4 days 7 hrs";
                        };
                        matchStatus =                         {
                            text = wait;
                        };
                        matchType =                         {
                            text = Private;
                        };
                        noOfPlayers =                         {
                            text = 2;
                        };
                        priorityInList =                         {
                            text = 0;
                        };
                        scoreToWin =                         {
                            text = 5;
                        };
                        timeDuration =                         {
                            text = "5 days";
                        };
                        turnChangesIn =                         {
                            text = 0;
                        };
                    },
                                        {
                        answerTime =                         {
                            text = 30;
                        };
                        challengerAppealsGranted =                         {
                            text = "0 of 16";
                        };
                        challengerHandle =                         {
                            text = manish;
                        };
                        challengerMatchesPlayed =                         {
                            text = 93;
                        };
                        challengerPic =                         {
                            text = "image.png";
                        };
                        challengerScore =                         {
                            text = 0;
                        };
                        challengerlosses =                         {
                            text = 0;
                        };
                        challengerwins =                         {
                            text = 4;
                        };
                        handle1MatchesPlayed =                         {
                            text = 0;
                        };
                        handle1Score =                         {
                            text = 0;
                        };
                        handle1losses =                         {
                            text = 0;
                        };
                        handle1wins =                         {
                            text = 0;
                        };
                        handle2MatchesPlayed =                         {
                            text = 0;
                        };
                        handle2Score =                         {
                            text = 0;
                        };
                        handle2losses =                         {
                            text = 0;
                        };
                        handle2wins =                         {
                            text = 0;
                        };
                        handle3MatchesPlayed =                         {
                            text = 0;
                        };
                        handle3Score =                         {
                            text = 0;
                        };
                        handle3losses =                         {
                            text = 0;
                        };
                        handle3wins =                         {
                            text = 0;
                        };
                        idMatch =                         {
                            text = 749;
                        };
                        isAppeal =                         {
                            text = false;
                        };
                        isFreeForm =                         {
                            text = false;
                        };
                        isMultichoiceQuestion =                         {
                            text = false;
                        };
                        isPublic =                         {
                            text = false;
                        };
                        matchName =                         {
                            text = gfhf;
                        };
                        matchStartThresholdTime =                         {
                            text = "4 days 6 hrs";
                        };
                        matchStatus =                         {
                            text = wait;
                        };
                        matchType =                         {
                            text = Public;
                        };
                        noOfPlayers =                         {
                            text = 2;
                        };
                        priorityInList =                         {
                            text = 0;
                        };
                        scoreToWin =                         {
                            text = 5;
                        };
                        timeDuration =                         {
                            text = "5 days";
                        };
                        turnChangesIn =                         {
                            text = 0;
                        };
                    }
                );
                "xmlns:ns2" = "http://services.tgs.com/";
            };
        };
        "xmlns:S" = "http://schemas.xmlsoap.org/soap/envelope/";
    };
}

我需要key" matchStartThresholdTime"的值。 我这样做:

NSDictionary *dictResult = [XMLReader dictionaryForXMLString:responseString error:nil];
                NSDictionary *Enveloper  = [dictResult objectForKey:@"S:Envelope"];
                NSDictionary *Body       = [Enveloper objectForKey:@"S:Body"];
                NSDictionary *profileDetails = [Body objectForKey:@"ns2:getMatchListResponse"];
                NSMutableArray *items = [profileDetails objectForKey:@"item"];

             //  NSLog(@"items===>%@",items);

               NSDictionary *temp1;

               for(temp1 in items)
               {

                   thresholdTime = [NSString stringWithFormat:@"%@",[[temp1 objectForKey:@"matchStartThresholdTime"]objectForKey:@"text"]];...thresholdTime is NSString object.......and this line causes exception

                   //NSLog(@"time is===>%@",[[temp1 objectForKey:@"matchStartThresholdTime"]objectForKey:@"text"]);
               }

这里有什么问题?

1 个答案:

答案 0 :(得分:0)

Temp1包含您期望的NSString对象,而不是NSDictionary

在处理JSON时,最好(双)检查收到的对象是否真的是您期望的类型。始终使用

if ([temp1 isKindOfClass:[NSDictionary class]]) ... 

左右。