NSString initWithData返回null [英] NSString initWithData returns null

查看:364
本文介绍了NSString initWithData返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 NSURLConnection 从网站提取数据,并在 NSMutableData 的实例中保存接收的数据。在 connectionDidFinishLoading 委托方法中,数据将转换为一个字符串,并调用NSString的适当方法:

  NSString * result = [[NSString alloc] initWithData:data 
encoding:NSUTF8StringEncoding]


b $ b

结果字符串为null。如果我使用 NSASCIIStringEncoding ,但是,我获得适当的字符串,虽然unicode字符乱码如预期。服务器的 Content-Type 标头不是指定UTF-8编码,但我已尝试过一些类似情况的不同网站,有字符串转换发生只是罚款。它似乎问题只与给定的Web服务有关,但我没有线索为什么。



在一个侧面的注意,是从一个API良好实践拉网页和数据

非常感谢!

解决方案

你说它绝对是UTF-8,但没有Content-Type头,你真的不知道。 (即使你有一个标题说,它仍然可能是错误的。)



我的猜测是,你的数据通常是ASCII,它总是作为UTF -8,但您有时试图解析实际在ISO 8859-1或Windows代码页1252中编码的数据。这些数据通常大多是ASCII,但是有些字节在0-127范围之外ASCII定义。 UTF-8将期望这样的字节形成在指定的范围序列内的代码单元序列,但是在其他编码中,任何字节,无论其值如何,都是其自身的完整字符。尝试将非ASCII非UTF-8数据解释为UTF-8将几乎总是得到错误的结果(错误的字符)或根本没有结果(不能解码;解码器返回 nil ),因为数据从来没有以UTF-8编码。



你应该首先尝试UTF-8,如果失败, 8859-1。如果您允许用户检索任何网页,您应该允许他们更改您用于解码数据的编码,以防他们发现它实际上是8859-9或codepage-1252或其他一些8位编码。 / p>

如果您要从特定服务器下载数据,特别是如果您对该服务器上运行的内容有影响,则应该提供准确的内容类型头文件和/或修复任何错误导致它提供不在UTF-8中的文本。


I am pulling data from a website via NSURLConnection and stashing the received data away in an instance of NSMutableData. In the connectionDidFinishLoading delegate method the data is convert into a string with a call to NSString's appropriate method:

NSString *result = [[NSString alloc] initWithData:data 
                                     encoding:NSUTF8StringEncoding]

The resulting string turns out to be a null. If I use the NSASCIIStringEncoding, however, I do obtain the appropriate string, albeit with unicode characters garbled up as expected. The server's Content-Type header does not specify the UTF-8 encoding, but I have attempted a number of different websites with a similar scenario, and there string conversion happens just fine. It seems like the problem only pertains to the given web service but I have no clue why.

On a side note, is pulling web pages and data from an API good practice, i.e. buffering the data, converting into a string, and manipulating the string afterwards?

Much appreciated!

解决方案

You say that it "is definitely UTF-8", but without a Content-Type header, you don't really know that. (And even if you did have a header saying that, it could still be wrong.)

My guess is that your data is usually ASCII, which always parses correctly as UTF-8, but you sometimes are trying to parse data that's actually encoded in ISO 8859-1 or Windows codepage 1252. Such data will generally be mostly ASCII, but with some bytes outside the 0–127 range ASCII defines. UTF-8 would expect such bytes to form a sequence of code units within a specified sequence of ranges, but in other encodings, any byte, regardless of value, is a complete character on its own. Trying to interpret non-ASCII non-UTF-8 data as UTF-8 will almost always get you either wrong results (wrong characters) or no results at all (cannot decode; decoder returns nil), because the data was never encoded in UTF-8 in the first place.

You should try UTF-8 first, and if it fails, use ISO 8859-1. If you're letting the user retrieve any web page, you should let them change the encoding you use to decode the data, in case they discover that it was actually 8859-9 or codepage-1252 or some other 8-bit encoding.

If you're downloading the data from a specific server, and especially if you have influence on what runs on that server, you should make it serve up an accurate Content-Type header and/or fix whatever bug is causing it to serve up text that isn't in UTF-8.

这篇关于NSString initWithData返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆