获取在 ViewController class 中启动应用程序的文件名?

Get name of file that launched an app in ViewController class?

我实现了 NSApplicationDelegate 方法 func application(_ sender: NSApplication, openFile filename: String) -> Bool 来获取双击以启动我的 macOS 应用程序的文件的名称 (Swift 4.x)。但是,似乎在 ViewController viewDidLoad() 之后调用了此方法,这是我所有初始化代码发生的地方。有没有办法在 ViewController class 中获取 viewDidLoad() 中的文件名,以便我可以在初始化代码中直接使用文件名?

更新:

我现在可以根据此 link 对 Swift 4.2 的建议将数据从 AppDelegate 传递到 ViewController

所以我的问题仍然是如何直接在 ViewDidLoad() 中获取文件名,或者能够通过 application(_, sender: NSApplication, openFile filename: String) -> Bool 获取文件名并将其传递到 ViewController 当调用 ViewDidLoad() 时。

解决了我的问题。我没有在 ViewController 中执行我的初始化代码,而是在 AppDelegate applicationDidFinishLaunching 中实现了代码,以便可以在应用程序启动时和第一个事件发生之前执行初始化。我相信这对于 AppDelegate 处理应用程序初始化来说是更合适的做法。本次代码调整既解决了ViewControllerAppDelegate之间传递数据的问题,也解决了ViewDidLoad().

后调用application(_, sender: NSApplication, openFile filename: String) -> Bool的问题