我正在尝试在swift中创建一个无界数组。我找到了分配Ints和基本Floats的无界数组的例子,但是当我尝试将数组转换为CGFloats数组时,我发现了一些问题。下面的snipet有一个名为vertices的数组,我相信我已经将它定义为CGFloats的无界数组。一旦我尝试索引CGfloats,编译器就会抱怨无法下标类型' [(GLfloat)]'索引类型'((Int32))'也许我不能真正理解错误告诉我的是什么。任何人都可以通过使用Int?
订阅CGfloat数组来解释这个问题private func makeSomeCoords(cx: Float, cy: Float, cz: Float, r: Float, p:Int32)
{
var theta1 = 0.0 as Float
var theta2 = 0.0 as Float
var theta3 = 0.0 as Float
var ex = 0.0 as GLfloat
var ey = 0.0 as GLfloat
var ez = 0.0 as GLfloat
var px = 0.0 as GLfloat
var py = 0.0 as GLfloat
var pz = 0.0 as GLfloat
var vertices = [GLfloat]() // <= My presumption is these are not declared correctly
var normals = [GLfloat]()
var texCoords = [GLfloat]()
var abs_r = r as Float
var abs_p = p as Int32
if( abs_r < 0.0 ) {
abs_r = -abs_r
}
if(abs_p < 0 ) {
abs_p = -abs_p
}
for var i = 0 as Int32; i < p/2; ++i {
var i_fl = Float(i) as Float
theta1 = i_fl * (Float(M_PI)*2.0) / Float(abs_p) - Float(M_PI_2)
theta2 = (i_fl + 1.0) * (Float(M_PI)*2) / Float(abs_p) - Float(M_PI_2)
for var j = 0 as Int32; j <= abs_p; ++j {
theta3 = Float(j) * (Float(M_PI)*2) / Float(abs_p)
ex = cosf(theta2) * cosf(theta3)
ey = sinf(theta2)
ez = cosf(theta2) * sinf(theta3)
px = cx + abs_r * ex
py = cy + abs_r * ey
pz = cz + abs_r * ez
vertices[((6*j)+(0%6))] = px <= Complains Cannot subscript a value of type '[(GLfloat)]' with an index of type '((Int32))'
vertices[((6*j)+(1%6))] = py
vertices[((6*j)+(2%6))] = pz
normals[(6*j)+(0%6)] = ex
normals[(6*j)+(1%6)] = ey
normals[(6*j)+(2%6)] = ez
}
}
}