NSData to NSString converstion problem!

前端 未结 3 1675
囚心锁ツ
囚心锁ツ 2021-02-06 11:34

I\'m getting an html file as NSData and need to parse it to extract some info. My approach was to convert it to NSString with UTF8 encoding (the html has non english characters,

3条回答
  •  不思量自难忘°
    2021-02-06 12:35

    Supposing you got a NSURLResponse* response and an NSData* data:

    CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef) [response textEncodingName]);
    NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
    
    NSString* string = [[NSString alloc] initWithData:data encoding:encoding];
    
    // Do stuff here..
    
    [string release];
    

提交回复
热议问题