我正在使用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!")
编辑: "今日扩展"和"应用程序"位于不同的上下文中,因此这是不可能的。