在iOS 7中转换编码字符

时间:2014-03-15 19:30:18

标签: ios objective-c

我有一个iOS 7应用程序正在调用Web服务,该服务返回带有编码字符的XML。起初只有少数几个,所以我只是这样替换它们:

- (NSString *)convertEncodedCharacters:(NSString *)html
{        
    // Handle escape characters
    html = [html stringByReplacingOccurrencesOfString:@"’" withString:@"'"];
    html = [html stringByReplacingOccurrencesOfString:@"‘" withString:@"‘"];
    html = [html stringByReplacingOccurrencesOfString:@"—" withString:@"-"];
    html = [html stringByReplacingOccurrencesOfString:@""" withString:@"\""];
    html = [html stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
    html = [html stringByReplacingOccurrencesOfString:@"&#x3C;" withString:@"<"];
    html = [html stringByReplacingOccurrencesOfString:@"&#x3E;" withString:@">"];
    html = [html stringByReplacingOccurrencesOfString:@"&#x2013;" withString:@"–"];
    html = [html stringByReplacingOccurrencesOfString:@"&#x201C;" withString:@"“"];
    html = [html stringByReplacingOccurrencesOfString:@"&#x201D;" withString:@"”"];
    html = [html stringByReplacingOccurrencesOfString:@"&#x201A;" withString:@"‚"];
    html = [html stringByReplacingOccurrencesOfString:@"&#xA0;" withString:@" "];

    return html;
}

但现在内容变得越来越复杂,我不断遇到新角色。有没有办法将所有编码的字符转换成人类可读的字符?

1 个答案:

答案 0 :(得分:0)

从GitHub检查这个类,正是你要找的那个

https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString+HTML.m