Swift: 如何将\U文字编码替换为纯UNICODE字符?

Swift: how to replace \U literal encoding into plain UNICODE characters?

假设我有以下来自服务器的字符串:

let uLiteralEncoded = "Derri\U00e8re le transfert d'Anthony Martial"

我想用字符串替换它,如下所示:

var plainEncoded = "Derrière le transfert d'Anthony Martial"

经过进一步的尝试,我终于找到了解决办法。

格式是具有 HTML 个实体的 HTML 格式(因此引号为 ' 并且变音符号带有 \U 编码)。

然后我写了一个 String 扩展,它构建了一个标准的 Swift 4 String 作为构造函数,如下所示:

extension String {

    /// String as HTML
    init(htmlEncodedString: String) {
        let encodedData = htmlEncodedString.data(using: String.Encoding.utf8)!
        let attributedOptions : [NSAttributedString.DocumentReadingOptionKey: Any] = [
            NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html,
            NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue
        ]
        do {
            let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)

            self.init(attributedString.string)
        }
        catch {
            self.init(htmlEncodedString)    // Something gone wrong, stick with the initial string
        }
    }
}