swift - How to create a matrix of CAShapeLayers? -
i have code:
var triangles: [[[cashapelayer]]] = array(repeating: array(repeating: array(repeating: 0, count: 2), count: 15), count: 15); but generates "cannot convert value of type..." compilation error.
how can solve that? want access cashapelayers this:
triangles[1][2][1].fillcolor = uicolor(red: 40/255, green: 73/255, blue: 80/255, alpha: 1).cgcolor; 
use optionals.
var triangles: [[[cashapelayer?]]] = array(repeating: array(repeating: array(repeating: nil, count: 2), count: 15), count: 15) now there's nil instead of 0, think hinting at. every triangles[x][y][z] optional type you'll have safely unwrap.
so have triangles[x][y][z] = cashapelayer() before object.
edit correction. @ooper
i thought more, , realized didn't really answer question.
so may use loops initialize (which pain), or every time access index:
if triangles[x][y][z] == nil {     triangles[x][y][z] = cashapelayer() } let bloop = triangles[x][y][z]! bloop.fillcolor = uicolor(... then pull out outside method becomes 1 liner. like:
func tri(at x: int, _ y: int, _ z: int) -> cashapelayer {     if triangles[x][y][z] == nil     {         triangles[x][y][z] = cashapelayer()     }     return triangles[x][y][z]! } then when using it:
tri(at: 1, 2, 1).fillcolor = ... of course, should pull triangles out , make property of class you're in, or can include in parameter list of 1 liner method.
Comments
Post a Comment