【问题标题】:Swift: "invalid unicode scalar" error when using unicode code point as String literalSwift:使用 unicode 代码点作为字符串文字时出现“无效的 unicode 标量”错误
【发布时间】:2017-08-05 23:05:44
【问题描述】:

当我尝试在我的字符串文字中包含一个 Unicode 代码点时,我收到“无效的 unicode 标量”错误:

let str1 = "\u{127881}"

这实际上是 ???? 的有效代码点表情符号。

为什么会出现错误,如何在我的字符串文字中包含 127881 Unicode 代码点?

【问题讨论】:

    标签: unicode swift3


    【解决方案1】:

    您应该使用十六进制代码,在本例中为 1f389

    let str1 = "\u{1f389}"
    

    但由于这是我们正在谈论的 Swift,您可以轻松地做到这一点:

    let str1 = "?"
    

    【讨论】:

      猜你喜欢
      • 2015-01-18
      • 1970-01-01
      • 2020-01-21
      • 2019-05-13
      • 2021-11-08
      • 2013-04-29
      • 2016-02-02
      • 2017-07-25
      • 1970-01-01
      相关资源
      最近更新 更多