user7237935
user7237935

Reputation:

Creates a string containing the given character issue

In the Apple Documentation :

init(_: Character)
Creates a string containing the given character.

c: The character to convert to a string.

Declaration
init(_ c: Character)

I try to create a string with a character using string init but I fail.I don't understand the declaration above.I want to create a string including "k".

Here is example:

String(_ c: "k") // fails
String("k" c: Character) // fails

What is the correct way acoording to declaration above.I don't understand what _: means in the declaration.

Can someone explain what the declaration means in human language ?

Upvotes: 2

Views: 25

Answers (1)

Luca Angeletti
Luca Angeletti

Reputation: 59496

init(_: Character)
     ^

means the parameter doesn't have an external name. So when you call that initializer you simply put the value, without a label.

Like this

let char: Character = "A"
let word = String(char)

Upvotes: 1

Related Questions