0

In Objective-C these are two declarations of array of pointers:

NSArray<MTKMesh *> *mtkMeshes;
NSArray<MDLMesh *> *mdlMeshes;

I am struggling declaring the equivalent in Swift 3.0.

1 Answer 1

2

MTKMesh and MDLMesh are classes (reference types). A variable of type MTKMesh in Swift is a reference to an object instance, i.e. what a variable of type MTKMesh * is in Objective-C.

Therefore you can simply declare

var mtkMeshes: [MTKMesh] = []
var mdlMeshes: [MDLMesh] = []

Each element of the array is a reference to an object instance:

let mesh1 = MDLMesh()
let mesh2 = MDLMesh()
mdlMeshes.append(mesh1)
mdlMeshes.append(mesh1)
mdlMeshes.append(mesh2)

print(mdlMeshes[0] === mdlMeshes[1]) // true
print(mdlMeshes[0] === mdlMeshes[2]) // false

The first two array elements reference the same object instance, the last array element references a different instance. (=== is the "identical-to" operator).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Martin. In your Swift equivalent I notice you have pre-allocated the arrays which is actually different then the Obj-C. For context, this is a snippet from code that uses mdlMeshes as an inout param to a function that allocates and populates that array. +1 for the '"===" tip.
@dugla: You can also declare var mtkMeshes: [MDLMesh]? and pass its address to a function func foo(meshes: inout [MDLMesh]?) which allocates and populates the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.