简体   繁体   English

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

[英]How to iterate through a tuple array with for loop in Swift

I have an array of tuples defined as:我有一组元组定义为:

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

when trying to go through it like this:当试图像这样经历它时:

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

I get this error message:我收到此错误消息:

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

What does it mean, and what am I doing wrong?这是什么意思,我做错了什么?

The type of points2D is not Array , but rather Array<(Int,Int)> , so let Swift infer the type: points2D 的类型不是Array ,而是Array<(Int,Int)> ,所以让 Swift 推断类型:

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

or set the correct type explicitly:或明确设置正确的类型:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM