gpt4 book ai didi

arrays - 如何在 Swift 中使用 for 循环遍历元组数组

转载 作者:搜寻专家 更新时间:2023-10-31 22:47:04 24 4
gpt4 key购买 nike

我有一个元组数组定义为:

var points2D:Array=[(1,1),(2,3),(4,3),(9,5),(3,3),(7,6),(5,6)]

当尝试像这样经历它时:

func foo(){
for (x,y) in points2D{
}
}

我收到此错误消息:

'τ_0_0' is not convertible to '(@lvalue Array, @lvalue Array)'

这是什么意思,我做错了什么?

最佳答案

points2D 的类型不是 Array ,而是 Array<(Int,Int)> ,所以让 Swift 推断类型:

 var points2D = [(1,1),(2,3),(4,3),(9,5),(3,3),(7,6),(5,6)]

或明确设置正确的类型:

var points2D:Array<(Int,Int)> = [(1,1),(2,3),(4,3),(9,5),(3,3),(7,6),(5,6)]

关于arrays - 如何在 Swift 中使用 for 循环遍历元组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35527382/

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