Using Swift to unescape unicode characters, ie \u1234

前端 未结 3 987
耶瑟儿~
耶瑟儿~ 2020-12-01 22:56

I have problems with special characters when using JSON in xcode 6 with swift

I found these codes in Cocoa/objective C to solve some problems converting accent but c

相关标签:
3条回答
  • 2020-12-01 23:32

    It's fairly similar in Swift, though you still need to use the Foundation string classes:

    let transform = "Any-Hex/Java"
    let input = "\\u5404\\u500b\\u90fd" as NSString
    var convertedString = input.mutableCopy() as NSMutableString
    
    CFStringTransform(convertedString, nil, transform as NSString, 1)
    
    println("convertedString: \(convertedString)")
    // convertedString: 各個都
    

    (The last parameter threw me for a loop until I realized that Boolean in Swift is a type alias for UInt - YES in Objective-C becomes 1 in Swift for these types of methods.)

    0 讨论(0)
  • 2020-12-01 23:36

    Swift 4 String extension

    extension String {
        var unescapingUnicodeCharacters: String {       
            let mutableString = NSMutableString(string: self)
            CFStringTransform(mutableString, nil, "Any-Hex/Java" as NSString, true)
    
            return mutableString as String
        }
    }
    
    0 讨论(0)
  • 2020-12-01 23:40

    Swift 3

    let transform = "Any-Hex/Java"
    let input = "\\u5404\\u500b\\u90fd" as NSString
    var convertedString = input.mutableCopy() as! NSMutableString
    
    CFStringTransform(convertedString, nil, transform as NSString, true)
    
    print("convertedString: \(convertedString)")
    // convertedString: 各個都
    
    0 讨论(0)
提交回复
热议问题