我有一组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
:
答案 0 :(得分:4)
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