把系统升级到 iOS9 以后  现在的项目里里使用Htmlparser 去解析 Html 文本发现乱码,跟踪代码到:

-(id)initWithString:(NSString*)string error:(NSError**)error 里

调整编码无效,最后发现是由于iOS9中“Tagged Pointer NSStrings” 造成的,具体的原因如下所示

Tagged Pointer NSStrings

Starting with iOS 9 certain strings (ones with a suitable length and encoding) on 64 bit architectures will now use a “tagged pointer” format where the string contents are stored directly in the pointer. This matches behavior introduced on OS X in 10.10 Yosemite.

Similarly to the OS X targets, passing a tagged NSString to functions such as CFStringGetCStringPtr or CFStringGetCharactersPtr will return NULL in cases where it may not have before. As before it is important to check for the NULL return value and use the corresponding buffer fetching function:

<tt>char buffer[BUFSIZE];
const char *ptr = CFStringGetCStringPtr(str, encoding);
if (ptr == NULL) {
    if (CFStringGetCString(str, buffer, BUFSIZE, encoding)) ptr = buffer;

In addition, this change enables index and range checking to be performed more often when working with strings. So you may see runtime exceptions due to this change. In almost all cases these exceptions point to bugs in code, so please take them seriously.

This change will also break any code which treats NS/CFStrings objects as pointers and attempts to dereference them.



-(id)initWithString:(NSString*)string error:(NSError**)error


if (self = [super init])


_doc = NULL;


if ([string length] &gt; 0)


CFStringEncoding cfenc = CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding);

CFStringRef cfencstr = CFStringConvertEncodingToIANACharSetName(cfenc);

const char *enc = CFStringGetCStringPtr(cfencstr, 0);

char buffer[255];

if (enc == NULL) {

if (CFStringGetCString(cfencstr, buffer, 255, kCFStringEncodingUTF8)) enc = buffer;



// _doc = htmlParseDoc((xmlChar*)[string UTF8String], enc);

int optionsHtml = HTML_PARSE_RECOVER;

optionsHtml = optionsHtml | HTML_PARSE_NOERROR; //Uncomment this to see HTML errors

optionsHtml = optionsHtml | HTML_PARSE_NOWARNING;

_doc = htmlReadDoc ((xmlChar*)[string UTF8String], NULL, enc, optionsHtml);





if (error) {

*error = [NSError errorWithDomain:@"HTMLParserdomain" code:1 userInfo:nil];





return self;





  1. Pingback引用通告: Cıvata