转换字符串unicode 8 swift

时间:2015-09-26 13:04:47

标签: swift string utf-8 nsdata

我是json的nsdata 当我输入字符串时我得到#1495;#1493;#1507; #1492;#1510;#1493;#1511; +&安培;在每个标签之前 我需要希伯来语 代码是

let json_url = NSURL(string: "http://itaypincas7.ipage.com/WigiTech-DB/createJSON.php")
    let jsonData = NSData(contentsOfURL: json_url!)

          do {
        let json = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary

        for (_, subJson) in json {
            let id = Int(subJson["id"] as! String)
            let name: String = subJson["name"] as! String
            let area: String = subJson["area"] as! String
            let latitude = CLLocationDegrees(CGFloat(Float(subJson["latitude"] as! String)!))
            let longitude = CLLocationDegrees(CGFloat(Float(subJson["longitude"] as! String)!))


            let beachNew = BeachNew(id: id!, name: name, area: area, latitude: latitude, longitude: longitude)

            beachesList.append(beachNew)
        }

1 个答案:

答案 0 :(得分:1)

您可以使用NSAttributedString替换HTML实体。这是解码name变量的示例。

let name: String = subJson["name"] as! String
let nameData = name.dataUsingEncoding(NSUTF8StringEncoding)!
let options: [String: AnyObject] = [
    NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
    NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
]
let attributedName = try NSAttributedString(data: nameData, options: options, documentAttributes: nil)
let decodedName = attributedName.string

decodedName的值会将所有实体替换为您正在寻找的希伯来字符。