2017-06-10 36 views
1

我有兩個對象的位置的對角線點的陣列,我希望x [1]去與y [1]。這是我做了什麼使用數組來創建對象的對角線使用for循環

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)等等...]這使得一個網格,我想要的是一條對角線。

回答

1

你想要做什麼是

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和做

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

沒有必要有相同的值的兩個數組

+0

感謝你的工作完全的我想要的方式。只要我被允許,我會盡快接受你的回答 – billy123

1

試試這個

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