以下のコードをxcode上で実行するとタイトルのエラーが出ます。
実行するとコンパスの針の方向計算の部分でエラーが出力されます。

どなたか修正方法がわかれば教えてください。

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    // ロケーションマネージャ
    var locationManager: CLLocationManager!
    // コンパスの針
    @IBOutlet weak var needleView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

    // ロケーションマネージャ生成
        locationManager = CLLocationManager()
        // ロケーションマネージャのデリゲート設定
        locationManager.delegate = self
        // 角度の取得開始
        locationManager.startUpdatingHeading()
    }

    // 角度の更新で呼び出されるデリゲートメソッド
    func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        // コンパスの針の方向計算
        needleView.transform = CGAffineTransform.init(rotationAngle: CGFloat(-newHeading.magneticHeading) * CGFloat.pi / 180)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}