合并字符串数组 - 调用下标时没有完全匹配
我收到 Xcode 错误
全局函数“zip”要求“String.Element”(又名“字符”)符合“序列”
array[i] = zip(array[i], arrayOfZeros)
上的
func expandedForm(_ num: Int) -> String {
let lenght = String(num).count
var array = Array(String(num))
var i = 0
while array.count > i {
let numberOfZeros = array.count - 1 - i
let arrayOfZeros = Array(repeating: "0", count: numberOfZeros)
let string = array[i]
array[i] = zip(array[i], arrayOfZeros)
i += 1
}
return ""
}
“序列”我正在尝试合并两个字符串数组,但我认为它们不是同一类型。在这方面需要一些帮助。
I'm getting an Xcode error
Global function 'zip' requires that 'String.Element' (aka 'Character') conform to 'Sequence'
on array[i] = zip(array[i], arrayOfZeros)
func expandedForm(_ num: Int) -> String {
let lenght = String(num).count
var array = Array(String(num))
var i = 0
while array.count > i {
let numberOfZeros = array.count - 1 - i
let arrayOfZeros = Array(repeating: "0", count: numberOfZeros)
let string = array[i]
array[i] = zip(array[i], arrayOfZeros)
i += 1
}
return ""
}
I'm trying to merge two array of strings, but I think they are not the same type. Need some help on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有一些问题。
var array = Array(String(num))
,如果将光标移到array
上,它是一个[String.Element]
,即[Character]
,一个Character
数组。所以
let string = array[i]
,这是一种误导,因为array[i]
是一个Character
,而不是String.
zip(_:_:)
等待两个序列 (~array),并提供一个Character
作为第一个参数。可能的解决方案:
制作
array
,一个String
的真实数组:然后:
或者创建一个新变量
var output
,它将是String
的数组code>String:并填充它:
You have some issues.
var array = Array(String(num))
, if you pass your cursor onarray
, it's a[String.Element]
, ie a[Character]
, an array aCharacter
.So
let string = array[i]
, that's misleading, becausearray[i]
is aCharacter
, not aString
.zip(_:_:)
awaits for two sequences (~array), and you give as first parameter aCharacter
.Possible solutions:
Make
array
, a real array ofString
:And then:
Or create a new variable
var output
that will be an array ofString
:And populate it: