AppDelegate is an important file in cocoa project, we could treat it as a bootstrap file and define some common variables or functions inside it.

Suppose we have these codes in our AppDelegate.swift

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate {

    lazy var persistentContainer: NSPersistentContainer = {
        let appName = "todo"
        let container = NSPersistentContainer(name: appName)
        container.loadPersistentStores(completionHandler: {
            (storeDescription, error) in if let error = error as NSError? {
        return container

now we need to access the persistentContainer in our viewcontroller, we could use the code bellow.

let appDelegate = NSApp.deletage as! AppDelegate
let persistentContainer =  appDelegate.persistentContainer