简体   繁体   中英

convert string unicode 8 swift

i was a nsdata from json when i put in string i get "#1495;#1493;#1507; #1492;#1510;#1493;#1511; +& Before each tag i need it in Hebrew the code is

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)
        }

You can use NSAttributedString to replace the HTML entities. Here's an example for decoding the name variable.

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

The value of decodedName will have all entities replaced with the Hebrew characters you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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