Group Group Group Group Group Group Group Group Group Forums

Metal Tutorial with Swift 3 Part 1: Getting Started

In this Metal tutorial, you will learn how to get started with Apple's 3D graphics API by rendering a simple triangle to the screen.

This is a companion discussion topic for the original entry at

Could not cast value of type ‘UIView’ (0x1ae7b8ed8) to ‘MTKView’ (0x1adbfa1d0). I an working with Mental. It compiles with no problem, but getting Run Time error. I checked Inspector and unable to find (MTKView) such class. import UIKit
import MetalKit

enum Colors {
static let wenderlichGreen = MTLClearColor (red: 0.0, green: 0.4,
blue: 0.21,
alpha: 1.0)}
class ViewController: UIViewController {

var metalView : MTKView{
    return view as! MTKView

var device: MTLDevice!
var commandQueue: MTLCommandQueue!
override func viewDidLoad() {
    // Do any additional setup after loading the view, typically from a nib.
    metalView.device = MTLCreateSystemDefaultDevice()
    device = metalView.device;
    metalView.clearColor = Colors.wenderlichGreen
    commandQueue = device.makeCommandQueue()
    let comandBuffer = commandQueue.makeCommandBuffer()
    let commandEncoder = comandBuffer.makeRenderCommandEncoder(descriptor: metalView.currentRenderPassDescriptor!)



Just to clarify things about this awesome tutorial, I’m an audio application software engineer and I was wondering to use Metal for this kind of application. Usually I work with matrix / vector of 4 000 000 data each which are processed with a lot of filters. So is it possible to take the advantages of Metal technology while working on audio data? I didn’t find any article talking about GPU processing for audio processing so I guess the answer is no but in this case why?

In the video (looking the code) import Metal was not included. After adding this line (import Metal) it worked.

I download the provided code, but got an error unable to compile (Swift Compiler Error Group, /Volumes/Backup/Metal/HelloMetal_1_Finished/HelloMetal/ViewController.swift:34:19: Use of undeclared type ‘CAMetalLayer’). I am using iPad pro 12.5 as a device.
This implies I am unable to create CAMetalLayer.

Had the same error. I fixed it by selecting ‘Generic iOS Device’ instead of an simulator.

Does this tutorial have a macOS variant too?

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]