模型 I/O – 如何使用 `makeVerticesUniqueAndReturnError()` 实例方法?

Model I/O – How to use `makeVerticesUniqueAndReturnError()` instance method?

一个实例方法 makeVerticesUnique() 修改了网格的顶点缓冲区,因此没有顶点被多个面共享。但它在 macOS 10.13 High Sierra 和 iOS 11:

中被弃用
mdlMesh.makeVerticesUnique()            /* deprecated in macOS 10.13 and iOS 11 */

现在开发者必须使用新的实例方法:

func makeVerticesUniqueAndReturnError() throws

但没有记录。 如何使用?

当我使用这个新的实例方法时 Xcode 给我一个错误:

'throws' may only occur before '->'

每当您在 developer.apple.com 或 Xcode 文档查看器中找不到文档时,请检查框架 headers 或 Swift 界面——它们通常有代码至少可以作为粗略形式的文档的评论。

在 Xcode 中,使用快速打开 (⌘⇧O) 并键入相关 header 的名称 (MDLMesh.h) 或其中的一个符号 (MDLMesh, makeVerticesUnique, etc).或者 ⌘-单击源代码中的其中一个符号,然后选择跳转到定义。 (如果此时您最终进入 Objective-C header 并希望查看 Swift 版本,请从文件顶部的相关项目菜单中选择生成的界面。)

在这种情况下,您会发现这两种方法在用法上是等效的(但新方法能够抛出错误):

/*!
 @method makeVerticesUnique:
 @abstract Deindexes the vertex array
 @discussion If any vertices are shared on multiple faces, duplicate those
             vertices so faces do not share vertices. The vertex buffer and index
             buffers on submeshes may grow to accomadate any vertices added.
 */
@available(OSX, introduced: 10.11, deprecated: 10.13)
open func makeVerticesUnique()


/*!
 @method makeVerticesUniqueAndReturnError:
 @abstract Deindexes the vertex array
 @discussion If any vertices are shared on multiple faces, duplicate those
 vertices so faces do not share vertices. The vertex buffer and index
 buffers on submeshes may grow to accomadate any vertices added.
 */
@available(OSX 10.13, *)
open func makeVerticesUniqueAndReturnError() throws

据推测,Apple 确定原始方法没有优雅地处理故障(fatal-error 停止?崩溃?产生错误的输出?不知道),并决定最好在出现问题时让调用者知道。

这个新的 instance methodtry! 关键字完美配合:

try! mdlMesh.makeVerticesUniqueAndReturnError()

在我的特定情况下,抛出方法不会在运行时抛出错误。因此,我可以在表达式前写入 try! 以禁用错误传播并将调用包装在不会抛出任何错误的运行时断言中。如果真的抛出一个错误,我会得到一个运行时错误。