Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 conversion from Int to String

Tags:

string

swift3

In Swift 3, the String structure does not seem to have an initializer init(_: Int) that will allow the conversion from an Int to a String. My question is why does let i = String(3) work? What String method or initializer is it calling? Thanks.

like image 468
Don Giovanni Avatar asked Oct 22 '16 23:10

Don Giovanni


1 Answers

It's calling init(_:) (or init(_:) for UnsignedInteger) arguments of the String class.

Rather than defining separate initializers for Int, Int64, Int32, Int16, Int8, UInt, UInt64, UInt32, UInt16, and UInt8, Apple made two generic initializers: one for SignedInteger types, and one for UnsignedInteger types.

like image 181
Alexander Avatar answered Sep 27 '22 22:09

Alexander