NSXmlParser 有时无法正确解析 xml

发布于 2024-11-14 09:04:25 字数 1820 浏览 0 评论 0原文

当我调用 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'

when I call API and parse response xml, sometime I got this error ..

    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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七禾 2024-11-21 09:04:25

请检查所有字符串数据是否有值。
如果某个字符串值为零,那么你会得到这样的错误。因此,还包括空值的条件。

Please check that, all the string data have values or not.
If some string value will be nil then you will get such error. So, include the condition for null value also.

枉心 2024-11-21 09:04:25

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

As the error says, you are calling stringByAppendingString: on an NSXMLParser. Search your project for stringByAppendingString: and make sure you are calling it on a string.

蓝梦月影 2024-11-21 09:04:25

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

编辑:

我假设您已将 .h 中的 currentElement 声明为 NSMutableArray 并在 分配 init 函数如下所示。

  currentElement =  [NSMutableArray alloc] initWithCapacity:10];

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

Look like, you are calling stringByAppendingString method on the object of NSXMLParser.

EDITED:

I assume, you have declared currentElement in your .h as NSMutableArray and alloced that in init function like below.

  currentElement =  [NSMutableArray alloc] initWithCapacity:10];

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
     [currentElement appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
     NSLog(@"%@",string);
  } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文