2017-10-12 6 views
-5

저는 Swift를 처음 사용하고 일부 코드를 이식하려고합니다. 저는 이전 프로젝트에서 이것을 가지고 있습니다 :Swift (4)에서 간단한 구조 초기화?

typedef struct { 
    float Position[3]; 
    float Normal[3]; 
    float TexCoord[2]; // New 
} iconVertex; 

const iconVertex iconVertices[] = { 
    {{0.0,0.0, 0.0}, {0, 0, 1.0}, {0, 0}}, 
    {{1.0, 0.0, 0.0}, {0, 0, 1.0}, {1, 0}}, 
    {{0.0, 1.0, 0.0}, {0, 0, 1.0}, {0, 1}}, 
    {{1.0, 1.0, 0.0}, {0, 0, 1.0}, {1, 1}}, 
}; 

Swift에서 동일한 어레이 초기화를 수행 할 방법이 있습니까? 감사합니다.

답변

1

Swift에서는 구조체를 사용하여 객체를 정의하고 초기화해야하는 매개 변수를받는 init 메소드를 만들 수 있습니다.

struct IconVertex { 
    var position: [Double] 
    var normal: [Double] 
    var textCoord: [Double] 

    init(position: [Double], normal: [Double], textCoord: [Double]) { 
     self.position = position 
     self.normal = normal 
     self.textCoord = textCoord 
    } 
} 

let iconVertices: [IconVertex] = [ 
IconVertex(position: [0.0,0.0, 0.0], normal: [0, 0, 1.0], textCoord: [0, 0]), 
IconVertex(position: [1.0, 0.0, 0.0], normal: [0, 0, 1.0], textCoord: [1, 0]), 
IconVertex(position: [0.0, 1.0, 0.0], normal: [0, 0, 1.0], textCoord: [0, 1]), 
IconVertex(position: [1.0, 1.0, 0.0], normal: [0, 0, 1.0], textCoord: [1, 1])] 
+5

'초기화'는 필요하지 않습니다. 'struct'를 사용하면 다른 것을 제공하지 않으면 자동으로 그러한 init을 얻습니다. – rmaddy

+0

좋은 지적! 하지만 그는 어떤 코드를 포팅했기 때문에'init '을 만드는 방법을 보여주는 것이 유용하다고 생각했다. – jvrmed

+0

아마도 속성 유형에 대해 array 대신에'Vector3D' 또는 튜플을 사용하고 싶을 것이다. –