合并字符串数组 - 调用下标时没有完全匹配

发布于 2025-01-16 21:05:11 字数 648 浏览 1 评论 0原文

我收到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宫墨修音 2025-01-23 21:05:11
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 ""
}

你有一些问题。

var array = Array(String(num)),如果将光标移到array上,它是一个[String.Element],即[Character],一个Character 数组。

所以 let string = array[i],这是一种误导,因为 array[i] 是一个 Character,而不是 String.

zip(_:_:) 等待两个序列 (~array),并提供一个 Character 作为第一个参数。

可能的解决方案:

制作array,一个String的真实数组:

var array = Array(String(num)).map { String($0) }

然后:

array[i] = array[i] + arrayOfZeros.joined()

或者创建一个新变量var output,它将是String的数组code>String:

var output: [String] = []

并填充它:

output.append(String(array[i] + arrayOfZeros.joined())
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 ""
}

You have some issues.

var array = Array(String(num)), if you pass your cursor on array, it's a [String.Element], ie a [Character], an array a Character.

So let string = array[i], that's misleading, because array[i] is a Character, not a String.

zip(_:_:) awaits for two sequences (~array), and you give as first parameter a Character.

Possible solutions:

Make array, a real array of String:

var array = Array(String(num)).map { String($0) }

And then:

array[i] = array[i] + arrayOfZeros.joined()

Or create a new variable var output that will be an array of String:

var output: [String] = []

And populate it:

output.append(String(array[i] + arrayOfZeros.joined())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文