Swift init(count:,repeatedValue :)是否有效?

时间:2022-07-03 23:28:46

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

从参考资料中测试过: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")

Playground执行失败:错误:: 5:14:错误:无法找到接受提供的参数的'init'的重载var string = String(count:5,repeatedValue:“a”)

Does this actually work?

这实际上有用吗?

6 个解决方案

#1


26  

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

看来你必须明确传入一个Character类型才能运行。这对我有用。

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.

编辑:我要跟bug一起去。以下编译就好了。

let array = Array(count: 5, repeatedValue: "a")

#2


7  

For the benefit of future searchers: as of Swift 3, use init(repeating:count:).

为了未来的搜索者的利益:从Swift 3开始,使用init(重复:计数:)。

#3


6  

This works just fine :

这很好用:

var str9 = String(count: 5,repeatedValue: Character("c"))

#4


1  

For anyone in swift 3.x its now something like this this will work like a charm.

对于swift 3.x中的任何人来说,现在这样的东西就像魅力一样。

var string = String(repeating: "a", count: 5)

#5


0  

I know this is an old question and already has an answer. However I think I know why String(count: 5, repeatedValue: "a") does not work.

我知道这是一个老问题,已经有了答案。但是我想我知道为什么String(计数:5,repeatedValue:“a”)不起作用。

The thing is String has two similar looking initialisers:

问题是String有两个类似的初始化器:

init(count: Int, repeatedValue: Character)
init(count: Int, repeatedValue: UnicodeScalar)

So in this case compiler can't tell whether a literal is a Character or UnicodeScalar, hence compile time error if you don't pass explicit Character. To confirm that "a" can be interpreted as UnicodeScalar you can check that this line compiles:

因此,在这种情况下,编译器无法判断文字是字符还是UnicodeScalar,因此如果不传递显式字符,则编译时错误。要确认“a”可以解释为UnicodeScalar,您可以检查此行是否编译:

let a: UnicodeScalar = "a"

#6


0  

Swift 3:

斯威夫特3:

var array = Array(repeating: 0, count: 5)

Output: [0, 0, 0, 0, 0]

输出:[0,0,0,0,0]

#1


26  

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

看来你必须明确传入一个Character类型才能运行。这对我有用。

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.

编辑:我要跟bug一起去。以下编译就好了。

let array = Array(count: 5, repeatedValue: "a")

#2


7  

For the benefit of future searchers: as of Swift 3, use init(repeating:count:).

为了未来的搜索者的利益:从Swift 3开始,使用init(重复:计数:)。

#3


6  

This works just fine :

这很好用:

var str9 = String(count: 5,repeatedValue: Character("c"))

#4


1  

For anyone in swift 3.x its now something like this this will work like a charm.

对于swift 3.x中的任何人来说,现在这样的东西就像魅力一样。

var string = String(repeating: "a", count: 5)

#5


0  

I know this is an old question and already has an answer. However I think I know why String(count: 5, repeatedValue: "a") does not work.

我知道这是一个老问题,已经有了答案。但是我想我知道为什么String(计数:5,repeatedValue:“a”)不起作用。

The thing is String has two similar looking initialisers:

问题是String有两个类似的初始化器:

init(count: Int, repeatedValue: Character)
init(count: Int, repeatedValue: UnicodeScalar)

So in this case compiler can't tell whether a literal is a Character or UnicodeScalar, hence compile time error if you don't pass explicit Character. To confirm that "a" can be interpreted as UnicodeScalar you can check that this line compiles:

因此,在这种情况下,编译器无法判断文字是字符还是UnicodeScalar,因此如果不传递显式字符,则编译时错误。要确认“a”可以解释为UnicodeScalar,您可以检查此行是否编译:

let a: UnicodeScalar = "a"

#6


0  

Swift 3:

斯威夫特3:

var array = Array(repeating: 0, count: 5)

Output: [0, 0, 0, 0, 0]

输出:[0,0,0,0,0]