ios - 在 Swift 中将 UInt32 (UTF-32) 转换为字符串

标签 ios string swift uint32

我有一个 UInt32 的数组值。我想将此数组转换为 String

这行不通:

let myUInt32Array: [UInt32] = [72, 101, 108, 108, 111, 128049]
let myString = String(myUInt32Array) // error
let myString = String(stringInterpolationSegment: myUInt32Array) // [72, 101, 108, 108, 111, 128049] (not what I want)

这些 SO 帖子显示 UTF8UTF16:

最佳答案

UnicodeScalarUInt32 的类型别名。因此,将您的 UInt32 值转换为 UnicodeScalar,然后将它们附加到 String

let myUInt32Array: [UInt32] = [72, 101, 108, 108, 111, 128049]

var myString: String = ""

for value in myUInt32Array {
    if let scalar = UnicodeScalar(value) {
        myString.append(Character(scalar))
    }
}

print(myString) // Hello🐱

关于ios - 在 Swift 中将 UInt32 (UTF-32) 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31421054/

相关文章:

arrays - 如何在 Swift 中将数组分成两半?

ios - CLLocationManager 没有为同一位置的不同 iPhone 提供相同的经纬度坐标

ios - 如何组合过滤器

ios - 如何使 VStack 可点击以在 SwiftUI 中显示警报

python - 运行长度编码项目未输出

c# - 巨大的字符串到 WinForm RichTextBox

ios - Cocoapods 正在安装旧的 Pod 版本

ios - 在手机上跟踪室内锻炼时保持 iOS 应用程序运行

ios - 如何在开发过程中从 xcode 托管按需资源

将长十六进制字符串转换为十进制字符串