gpt4 book ai didi

arrays - 在 Swift 3 中迭代带有索引的数组

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:06 24 4
gpt4 key购买 nike

我正在尝试用 Swift 3 中的索引迭代一个数组,但不断得到

Expression type '[Int]' is ambiguous without more context

这可以在 playground 中使用以下示例重现:

var a = [Int]()
a.append(1)
a.append(2)
// Gives above error
for (index, value) in a {
print("\(index): \(value)")
}

我不确定它要求什么上下文。

最佳答案

您忘记调用 a.enumerated(),它为您提供了 (index, value) 元组。 for value in a 是为您提供每个没有索引的元素。

关于arrays - 在 Swift 3 中迭代带有索引的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40352899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com