现在用swift写了一个新项目,发现String和NSString还是有很大不同的,在Unicode转码上遇到了问题,我写了一个扩展,把以前OC的转码用swift重写了一遍,发现也能用,下面把我用swift做Unicode转码的代码贴在这里,亲测好用~~
extension String {
var unicodeStr:String {
let tempStr1 = self.stringByReplacingOccurrencesOfString("\\u",withString: "\\U")
let tempStr2 = tempStr1.stringByReplacingOccurrencesOfString("\"",withString: "\\\"")
let tempStr3 = "\"".stringByAppendingString(tempStr2).stringByAppendingString("\"")
let tempData = tempStr3.dataUsingEncoding(NSUTF8StringEncoding)
var returnStr:String = ""
do {
returnStr = try NSPropertyListSerialization.propertyListWithData(tempData!,options: .Immutable,format: nil) as! String
} catch {
print(error)
}
return returnStr.stringByReplacingOccurrencesOfString("\\r\\n",withString: "\n")
}
}