How to get an object instance of UIApplication in swift, and it can be encapsulated into a component without reporting errors.

static var mainWindow: UIWindow {

    get {
        let app = UIApplication.shared.delegate as! AppDelegate;
        if app.window != nil {
            app.window!.backgroundColor = .white;
            app.window!.makeKeyAndVisible();
            return app.window!;
        }
        app.window = UIWindow.init(frame: UIScreen.main.bounds);
        app.window?.backgroundColor = .white;
        app.window?.makeKeyAndVisible();
        return app.window!;
    }
    set {
        let app = UIApplication.shared.delegate as! AppDelegate;
        app.window = newValue;

    }
}

drag it into the actual production of the project, encapsulate and report an error

  • ERROR | [iOS] xcodebuild: / Users/shang/Documents/GitHub/SwiftExpand/SwiftExpand/UIApplication+Helper.swift:77:57: error: use of undeclared type "AppDelegate"
Feb.22,2022
Menu