我对这个堆栈跟踪感到很困惑(只显示了令人困惑的部分):
-[NSXMLDocument length]: unrecognized selector sent to instance 0x10187e010
An uncaught exception was raised
-[NSXMLDocument length]: unrecognized selector sent to instance 0x10187e010
(
0 CoreFoundation 0x00007fff8f5d6286 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff9213bd5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8f6624ce -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff8f5c3133 ___forwarding___ + 371
4 CoreFoundation 0x00007fff8f5c2f48 _CF_forwarding_prep_0 + 232
5 CoreFoundation 0x00007fff8f548c66 CFDataGetLength + 118
6 CoreFoundation 0x00007fff8f5791df CFStringCreateFromExternalRepresentation + 31
7 asLJ 0x0000000100013828 +[stripHTML stripAllHtmlFromString:] + 212
特别是,我不明白调用在哪里CFStringCreateFromExternalRepresentation
发生,所以我不知道我的代码 ( +[stripHTML stripAllHtmlFromString:]
) 的哪一部分导致了异常。是什么导致调用CFStringCreateFromExternalRepresentation
?如果很明显,我做错了什么导致异常?将来,我该如何确定调用的是CFStringCreateFromExternalRepresentation
什么?
这是+[stripHTML stripAllHtmlFromString:]
:
+ (NSString *)stripAllHtmlFromString:(NSString *)inputString
{
// based on code from http://sugarmaplesoftware.com/25/strip-html-tags/#comment-71
NSError *theError = NULL;
NSString *modifiedInputString = [NSString stringWithFormat:@"%@\n\n\n\n\n\n\n\n\n\n\n\n",inputString]; // adding some spare newlines at the end to ensure that things will work even with a short non-HTML string
NSXMLDocument *theDocument = [[NSXMLDocument alloc] initWithXMLString:modifiedInputString
options:NSXMLDocumentTidyHTML
error:&theError];
NSString *theXSLTString = @"<?xml version='1.0' encoding='utf-8'?>"
"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xhtml='http://www.w3.org/1999/xhtml'>"
"<xsl:output method='text'/>"
"<xsl:template match='xhtml:head'></xsl:template>"
"<xsl:template match='xhtml:script'></xsl:template>"
"</xsl:stylesheet>";
NSData *theData = [theDocument objectByApplyingXSLTString:theXSLTString arguments:NULL error:&theError];
[theDocument release];
return [[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding] autorelease];
}