It works on the simulator, but I have macOS 11.1
and I still encountered this problem.
In my case, the problem I found was that the MTLCreateSystemDefaultDevice()
result needs to be explicitly assigned to an MTLDevice
variable, before passing it to the metalView.
This works:
class ViewController: UIViewController {
var metalView: MTKView {
return view as! MTKView
}
var device: MTLDevice!
var commandQueue: MTLCommandQueue!
override func viewDidLoad() {
override func viewDidLoad() {
super.viewDidLoad()
device = MTLCreateSystemDefaultDevice()
metalView.device = device
metalView.clearColor = MTLClearColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 1.0)
metalView.delegate = self
commandQueue = device.makeCommandQueue()
}
}
}
If we try to assign it directly to metalView.device
, it throws the error:
override func viewDidLoad() {
super.viewDidLoad()
metalView.device = MTLCreateSystemDefaultDevice()
metalView.clearColor = MTLClearColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 1.0)
metalView.delegate = self
commandQueue = device.makeCommandQueue()
}