Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用数组创建对象的对角线使用for in循环_Arrays_Swift - Fatal编程技术网

Arrays 使用数组创建对象的对角线使用for in循环

Arrays 使用数组创建对象的对角线使用for in循环,arrays,swift,Arrays,Swift,我有两个点阵列作为物体位置的对角线,我希望x[1]与y[1]匹配。这就是我所做的 for X in x{ for Y in y{ positions.append(CGPoint(x:X,y:Y)) } } 但这不起作用,因为它使数组[(1,1),(1,2),(1,3),(1,4),(2,1)等]形成一个网格,我想要的是一条对角线。试试这个 for i in (0..<x.count) { positions.append(CGPoint(x:x[i], y:y[i])) }

我有两个点阵列作为物体位置的对角线,我希望x[1]与y[1]匹配。这就是我所做的

for X in x{
for Y in y{

positions.append(CGPoint(x:X,y:Y))

 }
}
但这不起作用,因为它使数组[(1,1),(1,2),(1,3),(1,4),(2,1)等]形成一个网格,我想要的是一条对角线。

试试这个

for i in (0..<x.count) {
   positions.append(CGPoint(x:x[i], y:y[i]))
}

对于(0..中的i,您要做的是

for i in 0...4{//or whatever the total amount of numbers are in the array
    positions.append(CGpoint(x[i],y[i]))
}
这将使每个x与每个y配对。感觉x的每个点都与y中的点相同,你只需要x和do

for X in x{
    positions.append(CGPoint(x:X,y:X))
}

没有必要有两个值完全相同的数组

谢谢你按照我希望的方式工作。我会尽快接受你的回答