Using Swift for unescape Unicode characters, i.e. \ u1234

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

I found these codes in Cocoa / target C to solve some problems with accent conversion, but couldn't get it working in Swift. Any suggestions for using it? ... the best alternative suggestions would also be cool ...

thanks

NSString *input = @"\\u5404\\u500b\\u90fd"; NSString *convertedString = [input mutableCopy]; CFStringRef transform = CFSTR("Any-Hex/Java"); CFStringTransform((__bridge CFMutableStringRef)convertedString, NULL, transform, YES); NSLog(@"convertedString: %@", convertedString); // prints: 各個都, tada! 
+3
source share
2 answers

This is pretty similar to Swift, although you still need to use 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 into a loop until I realized that Boolean in Swift is an alias of type UInt - YES in Objective-C becomes 1 in Swift for these types of methods.)

+12
source

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: 各個都 
+3
source

Source: https://habr.com/ru/post/1260974/


All Articles