我有一个 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 帖子显示 UTF8
和 UTF16
:
最佳答案
UnicodeScalar
是 UInt32
的类型别名。因此,将您的 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/