在Swift中如何配置AutoreleasingUnsafeMutablePointer?

时间:2016-11-11 22:13:17

标签: objective-c swift scenekit metal modelio

对于iOS应用,我将一些Objective-C代码转换为Swift。

Objective-C代码使用带有此签名的方法:

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

以下是它的调用方式:

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

mtkMeshes = [MTKMesh newMeshesFromAsset:asset
                                 device:_device
                           sourceMeshes:&mdlMeshes
                                  error:&error];

我正在尝试将此转换为Swift,我认为我做错了因为方法调用总是失败。

上述方法的Swift版本:

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

我如何使用它:

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")
}

该方法应该填充两个数组。它没有这样做。我在这里错过了什么?

1 个答案:

答案 0 :(得分:4)

类型的参数
NSArray?

您可以使用&传递var sourceMeshes: NSArray? metalMeshList = try MTKMesh.newMeshes(from:asset, device:device, sourceMeshes: &sourceMeshes) 变量的地址,因此这应该有效:

int option = 0;

do {

printf("Menu\n");
printf("[1] Insert\n");
printf("[2] List\n");
printf("[0] Exit\n");
printf("\nOption: ");
scanf("%i",&option);

switch(option)
{
    case 1: {
    // code
    break; }

    case 2: {
    // code
    break; }

    default: {
    // error
    }
}

} while(option != 0);