Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Swift init(count:, repeatedValue:) work?

Tags:

Tested this from the reference: https://developer.apple.com/documentation/swift

var string = String(count: 5, repeatedValue: "a") // string is "aaaaa" 

I got this error:

Playground execution failed: error: :5:14: error: could not find an overload for 'init' that accepts the supplied arguments var string = String(count: 5, repeatedValue: "a")

Does this actually work?

like image 489
Lim Thye Chean Avatar asked Jun 28 '14 02:06

Lim Thye Chean


1 Answers

It seems that you have to explicitly pass in a Character type to it to function. This works for me.

let char = Character("a") let string = String(count: 5, repeatedValue: char) 

Although, there may be bug mixed in with all this as well. I believe the way you were doing this should have worked on its own. And I can't seem to get code completion on this initializer at all.

Edit: I'm going with bug. The following compiles just fine.

let array = Array(count: 5, repeatedValue: "a") 
like image 137
Mick MacCallum Avatar answered Nov 07 '22 23:11

Mick MacCallum