swift: JSON 更改设备语言后无法正常工作
swift: JSON not working correctlly after changing device language
我提出 JSON 请求获取 goole YouTube 搜索建议,如果我将阿拉伯字母作为搜索关键字传递,如果设备语言是英语,它可以正常工作,但如果我将其更改为阿拉伯语,我得到了这个留言:
JSON could not be serialized because of error:
The data couldn’t be read because it isn’t in the correct format.
代码:
//Prepare Keyword string
searchKeyword = (searchKeyword as NSString).replacingOccurrences(of: " ", with: "+")
//Prepare url string
var str: String = "http://suggestqueries.google.com/complete/search?ds=yt&client=firefox&hjson=t&q='\(searchKeyword)'"
str = str.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!
//Getting the data
Alamofire.request(str).responseJSON { (response) in
//If error
if let error = response.error {
print(error.localizedDescription)
}
//If successed
if let jsonArray = response.result.value as? NSArray {
//Use the data
}
}
备注:
当设备是英文界面时,我在 JSON 响应描述中得到了 Content-Type = charset=ISO-8859-1
但是当设备处于阿拉伯语界面时 Content-Type = charset=windows-1256
我添加了这一行,问题就解决了!!
UserDefaults.standard.set(["ar", "en"], forKey: "AppleLanguages")
我提出 JSON 请求获取 goole YouTube 搜索建议,如果我将阿拉伯字母作为搜索关键字传递,如果设备语言是英语,它可以正常工作,但如果我将其更改为阿拉伯语,我得到了这个留言:
JSON could not be serialized because of error: The data couldn’t be read because it isn’t in the correct format.
代码:
//Prepare Keyword string
searchKeyword = (searchKeyword as NSString).replacingOccurrences(of: " ", with: "+")
//Prepare url string
var str: String = "http://suggestqueries.google.com/complete/search?ds=yt&client=firefox&hjson=t&q='\(searchKeyword)'"
str = str.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!
//Getting the data
Alamofire.request(str).responseJSON { (response) in
//If error
if let error = response.error {
print(error.localizedDescription)
}
//If successed
if let jsonArray = response.result.value as? NSArray {
//Use the data
}
}
备注: 当设备是英文界面时,我在 JSON 响应描述中得到了 Content-Type = charset=ISO-8859-1 但是当设备处于阿拉伯语界面时 Content-Type = charset=windows-1256
我添加了这一行,问题就解决了!!
UserDefaults.standard.set(["ar", "en"], forKey: "AppleLanguages")