m81
m81

Reputation: 2317

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.

As part of the function that goes from [UInt8] -> String, I'm attempting to convert a single Int32 into a 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.

Upvotes: 4

Views: 6634

Answers (2)

user3441734
user3441734

Reputation: 17544

you can use that constructor this way

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

Upvotes: 0

fluidsonic
fluidsonic

Reputation: 4676

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.

Character expects a UnicodeScalar which can be constructed from a UInt32 (and also 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. The error message just isn't clear enough.

Upvotes: 1

Related Questions