Programmatic mac app

My notes on programatic macOS app. This code creates a pure programatic setup w/o storyboard (Uncheck use storyboard when you create the project) Since Xcode 11.2 you have to tick use XIB

The code:

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {
   @IBOutlet weak var window: NSWindow!
    * Creates the view
   lazy var view: NSView = createView()
   func applicationDidFinishLaunching(_ aNotification: Notification) {
      _ = view
extension AppDelegate {
   func createView() -> NSView {
      let contentRect = window.contentRect(forFrameRect: window.frame)/*size of win sans titlebar*/
      let view: View = .init(frame: contentRect)
      window.contentView = view
      view.layer?.backgroundColor = NSColor.white.cgColor
      return view
open class View: NSView {
   override open var isFlipped: Bool { return true }/*TopLeft orientation*/
   override public init(frame: CGRect) {
      super.init(frame: frame)
      Swift.print("hello world")
      self.wantsLayer = true/*if true then view is layer backed*/
    * Boilerplate
   public required init?(coder decoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")


Sometimes you have to set the signing certificate to run locally