简体   繁体   中英

How do I convert Objective-C array of pointers to Swift?

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 .

MTKMesh and MDLMesh are classes (reference types). A variable of type MTKMesh in Swift is a reference to an object instance, ie 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM