MTKView:commandBuffer.present 上没有更多上下文,表达式类型不明确

MTKView : Type of expression is ambiguous without more context on commandBuffer.present

在命令缓冲区对象上设置可绘制对象时出现编译时错误。

以下是我的函数,它是从 MTKView 子类的 draw 方法调用的。

fileprivate func executeMetalShader() {
        guard let pipelineState = self.pipelineState,
            let threadgroupsPerGrid = self.threadgroupsPerGrid,
            let threadsPerThreadgroup = self.threadsPerThreadgroup
        else { return }

        guard let drawable: CAMetalDrawable = self.currentDrawable else { fatalError("Failed to create drawable") }
        let commandBuffer = commandQueue?.makeCommandBuffer()
        let commandEncoder = commandBuffer?.makeComputeCommandEncoder()
        commandEncoder?.setComputePipelineState(pipelineState)

        commandEncoder?.setTexture(yTexture, index: 0)
        commandEncoder?.setTexture(uTexture, index: 1)
        commandEncoder?.setTexture(vTexture, index: 2)
        commandEncoder?.setTexture(outTexture, index: 3)

        commandEncoder?.dispatchThreadgroups(threadgroupsPerGrid,
                                             threadsPerThreadgroup: threadsPerThreadgroup)
        commandEncoder?.endEncoding()
        commandBuffer.present(drawable) //. Error: Type of expression is ambiguous without more context
        commandBuffer?.commit()
        print("shader execution finish")
    }

我做错了什么或者API Swift 5 有什么变化吗?

我想你漏了一个 ?。修复:

commandBuffer?.present(drawable)