2016-11-11 57 views
1

對於iOS應用程序,我將一些Objective-C代碼轉換爲Swift。在Swift中,如何配置AutoreleasingUnsafeMutablePointer?

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]; 

我試圖將其轉換爲斯威夫特,我想我做錯了,因爲方法調用總是失敗。

雨燕以上版本的方法:

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

要類型的參數

AutoreleasingUnsafeMutablePointer<NSArray?>? 

你可以通過與&一個NSArray?變量的地址,所以這應該工作:

var sourceMeshes: NSArray? 
metalMeshList = try MTKMesh.newMeshes(from:asset, device:device, 
             sourceMeshes: &sourceMeshes) 
相關問題