Search code examples
swiftmacosappdelegatensviewcontroller

Access to AppDelegate methods from NSViewController


I'm trying to execute a method present in AppDelegate from my NSViewController class. I tried this line to access to the AppDelegate :

let appDelegate = UIApplication.shared.delegate as! AppDelegate;

Without success : Use of unresolved identifier 'UIApplication'. Tried to import UIKit, doesn't works ... cause it's for IOS ?

PS : It's hard to learn how to develop in swift ... a lot of tutorials are for objective C, and the swifts can also be for IOS ...


Solution

  • NSViewController is clearly macOS.

    In macOS you get the application delegate with

    let appDelegate = NSApp.delegate as! AppDelegate
    

    NSApp is a shortcut for NSApplication.shared