Xcode;如何从另一个视图控制器调用 AppDelegate 中的函数



我正在使用Swift和最新的Xcode版本。

我在AppDelegate.swift有这段代码:

func testPrint(string: String) {
    print(string)
}

我可以像这样从文件ViewController.swift成功调用该函数:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.testPrint(string: "Hello!")

但是,如果我尝试从 TodayViewController.swift 调用该函数,这是针对苹果Today Extension的,我会收到以下错误:

使用未声明的类型"AppDelegate">

为什么呢?

尝试以下操作:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.testPrint(string: "Hello!")

编辑: "今日扩展"和"应用程序"位于不同的上下文中,因此这是不可能的。

最新更新