简体   繁体   English

编码ISO 8859-1以在UITableview中正确显示西班牙语文本

[英]Encoding ISO 8859-1 to display Spanish text correctly in UITableview

I am facing problem in converting the Spanish text that is fetched from the service in 我在转换从服务中获取的西班牙语文本时遇到问题

correct format. 正确的格式。 Server side they are encoding with ISO-8859-1 . 服务器端使用ISO-8859-1进行编码。 It is a xml service. 这是一个xml服务。 In my 在我的

iOS7 app, I am using TBXml parser to parse the data. iOS7应用程序,我正在使用TBXml解析器来解析数据。 The code is: 代码是:

NSString *XMLString = [[NSString alloc] initWithContentsOfURL:urlString encoding:NSISOLatin1StringEncoding error:nil];

TBXML *tbxml = [[TBXML alloc] initWithXMLString:XMLString];

I am parsing this data, but the when there are Spanish characters like " BEBÉS Y " the my 我正在解析此数据,但是当有西班牙语字符如“ BEBÉSY ”时,

string will be " BEB…SY " . 字符串将为“ BEB…SY ”。 And " øPor quÈ albergamos a alborotadores? " instead of " ¿Por qué albergamos a alborotadores? " . 而不是“ ¿ Porquéalbergamos a alborotadores? ”代替。 Please help 请帮忙

You should download the XML data as binary ( NSData ) and let the parser handle the encoding. 您应该将XML数据下载为二进制( NSData ),并让解析器处理编码。

NSData *data = [NSData dataWithContentsOfURL:url];

NSError *error;
TBXML *tbxml = [TBXML tbxmlWithXMLData:data error:&error];

Note the XML should have the content encoding as the first line so there is no need to specify an encoding in code. 请注意,XML应该将内容编码作为第一行,因此无需在代码中指定编码。

Try with NSUTF8StringEncoding 尝试使用NSUTF8StringEncoding

NSString *XMLString = [[NSString alloc] initWithContentsOfURL:urlString encoding:NSUTF8StringEncoding error:nil];

TBXML *tbxml = [[TBXML alloc] initWithXMLString:XMLString];

UPDATE: 更新:

NSData *dataContent = [[NSData dataWithContentsOfURL:urlString];
NSString *XMLString = [[NSString alloc] initWithData:dataContent encoding:NSISOLatin1StringEncoding];

TBXML *tbxml = [[TBXML alloc] initWithXMLString:XMLString];

UPDATE 2: Try with data initialization 更新2:尝试进行数据初始化

NSData *dataContent = [[NSData dataWithContentsOfURL:urlString];

TBXML *tbxml = [[TBXML alloc] initWithXMLData:XMLString];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM