方法将解析空节点XML文件或2+,但如果只存在1个节点则崩溃

时间:2014-02-17 19:56:03

标签: ios objective-c xml

我的应用程序运行良好,我现在唯一的问题是实时数据查看器,如果没有发生任何事情,XML文件为空,如果它有2或2000个事件,应用程序现在不显示任何内容正在发生,它显示在我的实时数据查看器中,如果只有一个事件发生,当我到达那个视图时,我的应用程序崩溃。

我比较了XML结构,它们对于1或2个事件是相同的。

我在下面包含了我的XML解析器方法,我的cellForRowAtIndexPath和我的numberOfRows方法。

那里有人可以看到问题是什么吗?我再次提醒你,如果我的XML文件只有一个Match节点,这只会崩溃。适用于0或100000,但不适用于1。

最后你还会找到崩溃报告,我希望它有助于找到问题并修复它。

谢谢。

  -(void) parseXMLLiveMatch
    {

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"something.xml"]];

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSString *xmlString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    NSDictionary *xml = [NSDictionary dictionaryWithXMLString:xmlString];

    NSMutableArray *items = [xml objectForKey:@"Match"];

    NSMutableArray *newLiveMatchArray = [[NSMutableArray alloc] init];

    for (NSDictionary *dict in items) {
        NSLog(@"%@ %@", [dict class], dict);
        LiveMatchObject *myMatches = [LiveMatchObject matchesFromXMLDictionary:dict];
        [newLiveMatchArray addObject:myMatches];

    }

    NSNull *nullValue = [NSNull null];

    [newLiveMatchArray insertObject:nullValue atIndex:0];

    [self setTableDataLiveMatch:newLiveMatchArray];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"LiveIdent";

    LiveViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    LiveMatchObject *item = [tableDataLiveMatch objectAtIndex:(int)([indexPath row]/2)];

...
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tableDataLiveMatch.count * 2;
}



2014-02-17 14:40:56.419 Liga Zon Sagres Companion[2539:70b] __NSCFString HomeGoals
2014-02-17 14:40:56.420 Liga Zon Sagres Companion[2539:70b] -[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x10d03d7f0
2014-02-17 14:40:56.424 Liga Zon Sagres Companion[2539:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x10d03d7f0'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001019df795 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000101742991 objc_exception_throw + 43
    2   CoreFoundation                      0x0000000101a70bad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001019d109d ___forwarding___ + 973
    4   CoreFoundation                      0x00000001019d0c48 _CF_forwarding_prep_0 + 120
    5   Liga Zon Sagres Companion           0x00000001000014da +[LiveMatchObject matchesFromXMLDictionary:] + 138
    6   Liga Zon Sagres Companion           0x0000000100003da6 -[LiveTableViewController parseXMLLiveMatch] + 790
    7   Liga Zon Sagres Companion           0x0000000100003a48 -[LiveTableViewController viewWillAppear:] + 88
    8   UIKit                               0x00000001004943f4 -[UIViewController _setViewAppearState:isAnimating:] + 394
    9   UIKit                               0x00000001004c0ce5 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 524
    10  UIKit                               0x00000001004bd155 -[UITabBarController _setSelectedViewController:] + 259
    11  UIKit                               0x00000001004c046c -[UITabBarController _tabBarItemClicked:] + 248
    12  UIKit                               0x00000001003a6096 -[UIApplication sendAction:to:from:forEvent:] + 80
    13  UIKit                               0x00000001003a6044 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
    14  UIKit                               0x00000001005fb4cc -[UITabBar _sendAction:withEvent:] + 420
    15  UIKit                               0x00000001003a60ae -[UIApplication sendAction:to:from:forEvent:] + 104
    16  UIKit                               0x00000001003a6044 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
    17  UIKit                               0x000000010047a450 -[UIControl _sendActionsForEvents:withEvent:] + 203
    18  UIKit                               0x00000001003a6096 -[UIApplication sendAction:to:from:forEvent:] + 80
    19  UIKit                               0x00000001003a6044 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
    20  UIKit                               0x000000010047a450 -[UIControl _sendActionsForEvents:withEvent:] + 203
    21  UIKit                               0x00000001004799c0 -[UIControl touchesEnded:withEvent:] + 530
    22  UIKit                               0x00000001003dac15 -[UIWindow _sendTouchesForEvent:] + 701
    23  UIKit                               0x00000001003db633 -[UIWindow sendEvent:] + 988
    24  UIKit                               0x00000001003b4fa2 -[UIApplication sendEvent:] + 211
    25  UIKit                               0x00000001003a2d7f _UIApplicationHandleEventQueue + 9549
    26  CoreFoundation                      0x000000010196eec1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    27  CoreFoundation                      0x000000010196e792 __CFRunLoopDoSources0 + 242
    28  CoreFoundation                      0x000000010198a61f __CFRunLoopRun + 767
    29  CoreFoundation                      0x0000000101989f33 CFRunLoopRunSpecific + 467
    30  GraphicsServices                    0x000000010300b3a0 GSEventRunModal + 161
    31  UIKit                               0x00000001003a5043 UIApplicationMain + 1010
    32  Liga Zon Sagres Companion           0x000000010001fce3 main + 115
    33  libdyld.dylib                       0x000000010232b5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

干杯谢谢。

如被要求:

@implementation LiveMatchObject

@synthesize homeGoals, awayGoals, homeName, awayName, matchDate, matchMinute, matchStatus, homeTeamLogo, awayTeamLogo, spectators, round, homeStartingGoalie, homeStartingDefense, homeStartingMiddlefield, homeStartingFoward, homeFormation, awayStartingGoalie, awayStartingDefense, awayStartingMiddlefield, awayStartingFoward, awayFormation;


+(LiveMatchObject *)matchesFromXMLDictionary:(NSDictionary *)dict{

    LiveMatchObject *object =[[LiveMatchObject alloc]init];

    object.homeGoals = dict[@"HomeGoals"]; 
    object.awayGoals = dict[@"AwayGoals"];
    object.homeName = dict[@"Hometeam"];
    object.awayName = dict[@"Awayteam"];
    object.matchDate = dict[@"Date"];
    object.matchMinute = dict[@"Time"];
    object.matchStatus = [UIImage imageNamed:dict[@"Time"]];
    object.homeTeamLogo = [UIImage imageNamed:dict[@"HomeTeam_Id"]];
    object.awayTeamLogo = [UIImage imageNamed:dict[@"AwayTeam_Id"]];
    object.spectators = dict[@"Spectators"];
    object.round = dict[@"Round"];
    object.homeFormation = dict[@"HomeTeamFormation"];
    object.homeStartingGoalie = dict[@"HomeLineupGoalkeeper"];
    object.homeStartingMiddlefield = dict[@"HomeLineupMidfield"];
    object.homeStartingFoward = dict[@"HomeLineupForward"];
    object.awayFormation = dict[@"AwayTeamFormation"];
    object.awayStartingGoalie = dict[@"AwayLineupGoalkeeper"];
    object.awayStartingMiddlefield = dict[@"AwayLineupMidfield"];
    object.awayStartingFoward = dict[@"AwayLineupForward"];

    return object;

}

@end

XML

<CREPERR.COM >
<Match>
<Id>327080</Id>
<Date>2014-02-17T12:00:00-08:00</Date>
<League>Primeira Liga</League>
<Round>19</Round>
<Spectators/>
<Hometeam>Estoril-Praia</Hometeam>
<HomeTeam_Id>529</HomeTeam_Id>
<Awayteam>Braga</Awayteam>
<AwayTeam_Id>521</AwayTeam_Id>
<Time>9'</Time>
<HomeGoals>0</HomeGoals>
<AwayGoals>0</AwayGoals>
<HomeGoalDetails/>
<AwayGoalDetails/>
<HomeLineupGoalkeeper>Vagner</HomeLineupGoalkeeper>
<AwayLineupGoalkeeper>Eduardo</AwayLineupGoalkeeper>
<HomeLineupDefense>Yohan Tavares; Mano; Babanco; Ruben;</HomeLineupDefense>
<AwayLineupDefense>
Nurio; Aleksandar Miljkovic; Nuno André Coelho; Vincent Sasso;
</AwayLineupDefense>
<HomeLineupMidfield>
Javier Balboa; Goebel Evandro; Carlitos; Diogo Amado; Goncalo;
</HomeLineupMidfield>
<AwayLineupMidfield>
Custodio; Mauro; Leandro Kappel; Felipe Pardo; Alan;
</AwayLineupMidfield>
<HomeLineupForward>Seba;</HomeLineupForward>
<AwayLineupForward>Raul Andrei Rusescu;</AwayLineupForward>
<HomeSubDetails/>
<AwaySubDetails/>
<HomeTeamFormation>4-2-3-1</HomeTeamFormation>
<AwayTeamFormation>4-2-3-1</AwayTeamFormation>
<Location>Estádio António Coimbra da Mota</Location>
<Stadium>Estádio António Coimbra da Mota</Stadium>
<HomeTeamYellowCardDetails/>
<AwayTeamYellowCardDetails/>
<HomeTeamRedCardDetails/>
<AwayTeamRedCardDetails/>
</Match>
<AccountInformation>
Data requested at 2/17/2014
</AccountInformation>
</CREPERR.COM>

1 个答案:

答案 0 :(得分:0)

您正在崩溃,因为当数组中只有一个对象时,dict对象是NSString,而不是NSDictionaryNSString未响应string[@"Something"]界面,因此会导致您的应用程序崩溃。

测试此方法的一种方法是:

+(LiveMatchObject *)matchesFromXMLDictionary:(NSDictionary *)dict{
    if([dict isKindOfClass:[NSDictionary class]])
    {
        //This is a valid dictionary
    }
    else
    {
        //This is not a dictionary object, do something different
    }
}

为什么你在这里得到NSString而不是NSDictionary我不知道,你如何处理它以同样的方式获取数据是另一个问题。你将不得不在那里设置一个断点,看看你得到了什么NSString以及如何处理它。