dugla dugla - 1 year ago 134
Objective-C Question

In Swift how do I configure a AutoreleasingUnsafeMutablePointer?

For an iOS app I am converting some Objective-C code to Swift.

The Objective-C code uses a method with this signature:

+ (nullable NSArray<MTKMesh*>*)newMeshesFromAsset:(nonnull MDLAsset *)asset
device:(nonnull id<MTLDevice>)device
sourceMeshes:(NSArray<MDLMesh*>* __nullable * __nullable)sourceMeshes
error:(NSError * __nullable * __nullable)error;

Here is how it is called:

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

mtkMeshes = [MTKMesh newMeshesFromAsset:asset

I am trying to convert this to Swift and I think I am doing it wrong because the method call always fails.

The Swift version of the above method:

open class func newMeshes(from asset: MDLAsset, device: MTLDevice, sourceMeshes: AutoreleasingUnsafeMutablePointer<NSArray?>?) throws -> [MTKMesh]

How I use it:

do {

var myPointer: AutoreleasingUnsafeMutablePointer<NSArray?>? = nil
myPointer = AutoreleasingUnsafeMutablePointer<NSArray?>.init(&modelIOMeshList)

metalMeshList = try MTKMesh.newMeshes(from:asset, device:device, sourceMeshes: myPointer)

} catch {
fatalError("Error: Can not create Metal mesh from Model I/O asset")

The method is supposed to populate the two array. It does not do that. What have I missed here?

Answer Source

To a parameter of type


you can pass the address of a NSArray? variable with &, so this should work:

var sourceMeshes: NSArray?
metalMeshList = try MTKMesh.newMeshes(from:asset, device:device,
                                      sourceMeshes: &sourceMeshes)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download