斯威夫特的希伯来语字符 [英] Hebrew Characters in Swift
问题描述
我有一个应用程序可以下载网站的html,其中包含希伯来语字符,这是代码:
I have an app that downloads the html of a website the contains characters in hebrew, this is the code:
let newurl = NSURL(string: "http://www.handasaim.co.il")
var error: NSError?
let html = NSString(contentsOfURL: newurl!, encoding: NSASCIIStringEncoding, error: &error)
if (error != nil) {
println("Whoops, something went wrong")
} else {
println(html)
}
我的问题是希伯来语字符显示为杂乱无章:òøëúùòåú-éåíùéùé
My problem is that the hebrew characters show up as gibrish: òøëú ùòåú - éåí ùéùé
如何下载html并获取正确的字符?
How can I download the html and get the correct characters?
推荐答案
此服务器的响应编码为"Windows Hebrew".
This server's response encoding is "Windows Hebrew".
我正在使用CoreFoundation查找等效的正确String编码:
I'm using CoreFoundation to find the correct String encoding equivalent:
let newurl = NSURL(string: "http://www.handasaim.co.il")
var error: NSError?
let encoding = CFStringConvertEncodingToNSStringEncoding(UInt32(CFStringEncodings.WindowsHebrew.rawValue))
let html = String(contentsOfURL: newurl!, encoding: encoding, error: &error)
结果:
תיכוןלהנדסאיםהרצליהלידאוניברסיטתתלאביב
תיכון להנדסאים הרצליה ליד אוניברסיטת תל אביב
迅速2更新
if let newurl = NSURL(string: "http://www.handasaim.co.il") {
let encoding = CFStringConvertEncodingToNSStringEncoding(UInt32(CFStringEncodings.WindowsHebrew.rawValue))
if let html = try? String(contentsOfURL: newurl, encoding: encoding) {
print(html)
}
}
注意:请看一下Martin R的方法,以自动检测服务器响应的编码.我针对此解决方案的方法是检查卷曲的标头.
Note: have a look at Martin R's method to detect the encoding of the server response automatically. My method for this solution was to inspect the headers with curl.
这篇关于斯威夫特的希伯来语字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!