首页 > 解决方案 > 模型 I/O – 如何使用 `makeVerticesUniqueAndReturnError()` 实例方法?

问题描述

一个实例方法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 '->'

标签: swiftscenekitaugmented-realityarkitscenekit-modelio

解决方案


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

在 Xcode 中,使用 Open Quickly (⌘⇧O) 并输入相关标题的名称 ( MDLMesh.h) 或其中的符号之一 ( MDLMesh, makeVerticesUnique, etc)。或者 ⌘-单击源中的这些符号之一,然后选择跳转到定义。(如果此时您在 Objective-C 头文件中结束并希望查看 Swift 版本,请从文件顶部的相关项目菜单中选择 Generated Interface。)

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

/*!
 @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 确定原始方法没有优雅地处理故障(致命错误停止?崩溃?产生错误输出?不知道)并决定最好让调用者知道什么时候出现问题。


推荐阅读