1

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

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

4

1 回答 1

4

到类型的参数

AutoreleasingUnsafeMutablePointer<NSArray?>?

您可以使用 传递NSArray?变量的地址&,因此应该可以:

var sourceMeshes: NSArray?
metalMeshList = try MTKMesh.newMeshes(from:asset, device:device,
                                      sourceMeshes: &sourceMeshes)
于 2016-11-11T22:24:27.880 回答