如何在Swift中从View Controller获取当前UIApplication的实例?
此代码出现Use of unresolved identifier 'sharedApplication'
错误:
let app = sharedApplication as UIApplication
答案 0 :(得分:63)
编辑 Swift 4.x
let app = UIApplication.shared.delegate as! AppDelegate
也许试试:
let app = UIApplication.sharedApplication()
编辑(斯威夫特3):
应该注意,在Swift 3中,为了实现这一点,您需要访问shared
属性:
let app = UIApplication.shared
答案 1 :(得分:10)
你可以通过
来完成let app = UIApplication.sharedApplication()