简体   繁体   English

Swift错误:无法将'Int32'类型的值转换为预期的参数类型'Int32'

[英]Swift error: cannot convert value of type 'Int32' to expected argument type 'Int32'

I'm attempting to write a pair of functions to convert a String into a [UInt8] byte array and back again. 我正在尝试编写一对函数来将String转换为[UInt8]字节数组并再次返回。

As part of the function that goes from [UInt8] -> String , I'm attempting to convert a single Int32 into a Character . 作为来自[UInt8] -> String的函数的一部分,我试图将单个Int32转换为Character

let num : Int32 = 5
let char = Character(_builtinUnicodeScalarLiteral: num)

But I'm getting this strange error: 但是我收到了这个奇怪的错误:

error: cannot convert value of type 'Int32' to expected argument type 'Int32'
let char = Character(_builtinUnicodeScalarLiteral: num)
                                                   ^~~

EDIT: I managed to write my functions using different code, but I'm still curious about the meaning of the error. 编辑:我设法使用不同的代码编写我的函数,但我仍然对错误的含义感到好奇。

You should avoid using types, methods and initializers prefixed with _ as they are private and an implementation detail. 您应该避免使用前缀为_类型,方法和初始值设定项,因为它们是私有的和实现细节。 The same goes for anything with builtin in the name. 对于builtin名称的任何内容都是如此。

Character expects a UnicodeScalar which can be constructed from a UInt32 (and also UInt8 ): Character期望一个UnicodeScalar能够从构建UInt32 (并且也UInt8 ):

let num : UInt32 = 65
let g = Character(UnicodeScalar(num))
print(g) // prints 'A'

Your error occurrs because you pass an Int32 while Swift expects an Builtin.Int32 , which is a different type. 您的错误发生,因为您传递Int32而Swift期望一个不同类型的Builtin.Int32 The error message just isn't clear enough. 错误消息不够清楚。

you can use that constructor this way 你可以用这种方式使用那个构造函数

let num: Int32 = 5
let g = Character(_builtinUnicodeScalarLiteral: num.value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM