NSXmlParser有时无法正确解析xml

时间:2011-06-04 11:08:43

标签: iphone nsxmlparser

当我调用API并解析响应xml时,有时我遇到了这个错误..

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSXMLParser stringByAppendingString:]: unrecognized selector sent to instance 0x6a22080'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x014c7be9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x012bc5c2 objc_exception_throw + 47
    2   CoreFoundation                      0x014c96fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x01439366 ___forwarding___ + 966
    4   CoreFoundation                      0x01438f22 _CF_forwarding_prep_0 + 50
    5   CityDeal24                          0x0002520e -[CategoryXmlHandler parser:foundCharacters:] + 112
    6   Foundation                          0x001e8dd4 _characters + 235
    7   libxml2.2.dylib                     0x018371fb xmlParseCharData + 287
    8   libxml2.2.dylib                     0x01845a6f xmlParseChunk + 3730
    9   Foundation                          0x001e921a -[NSXMLParser parse] + 321
    10  CityDeal24                          0x00025030 -[CategoryXmlHandler parseXML:apiUrl:] + 444
    11  CityDeal24                          0x00004ac6 -[DagenDealsViewController downloadCategories] + 227
    12  CityDeal24                          0x00006708 -[DagenDealsViewController doInBackgroundWhenViewWillLoad] + 114
    13  Foundation                          0x0011ad4c -[NSThread main] + 81
    14  Foundation                          0x0011acd8 __NSThread__main__ + 1387
    15  libSystem.B.dylib                   0x9821c85d _pthread_start + 345
    16  libSystem.B.dylib                   0x9821c6e2 thread_start + 34
)
terminate called after throwing an instance of 'NSException'

3 个答案:

答案 0 :(得分:0)

请检查一下,所有字符串数据都有值。 如果某个字符串值为nil,那么您将收到此类错误。因此,也包括空值的条件。

答案 1 :(得分:0)

如错误所示,您在stringByAppendingString:上呼叫NSXMLParser。在您的项目中搜索stringByAppendingString:,并确保在字符串中调用它。

答案 2 :(得分:0)

看起来,您在stringByAppendingString的对象上调用NSXMLParser方法。

编辑:

我认为,您已将 .h 中的currentElement声明为NSMutableArray已分配 init 功能如下。

  currentElement =  [NSMutableArray alloc] initWithCapacity:10];

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
     [currentElement appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
     NSLog(@"%@",string);
  }