【问题标题】:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFString objectForKey:]
【发布时间】:2013-04-27 14:31:53
【问题描述】:

我的 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/";
    };
}

我需要键“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"]);
               }

这里有什么问题?

【问题讨论】:

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


    【解决方案1】:

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

    在处理 JSON 时,明智的做法是(双重)检查接收到的对象是否真的是您期望的类型。始终使用

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

    左右。

    【讨论】:

    • 它是 XML,而不是 JSON——生成的数据结构的 NSLog 类似于 JSON,但如果仔细观察会更复杂。但是您的评论认为仔细检查对象类型通常是明智的,尤其是在调试新场景时。
    • 你是对的。我被数据结构的类 json 输出分心了。但是,无论如何,明智的做法是仔细检查收到的内容是否符合预期。
    猜你喜欢
    • 2012-07-12
    • 2018-04-05
    • 2021-09-17
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多