如何在 iOS 上的 Swift 中获取 sharedApplication?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24114747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get sharedApplication in Swift on iOS?
提问by Evgenii
How can I get instance of current UIApplication from View Controller in Swift?
如何从 Swift 中的 View Controller 获取当前 UIApplication 的实例?
I am getting Use of unresolved identifier 'sharedApplication'
error with this code:
我收到Use of unresolved identifier 'sharedApplication'
此代码错误:
let app = sharedApplication as UIApplication
回答by Darren
EDIT Swift 4.x
编辑Swift 4.x
let app = UIApplication.shared.delegate as! AppDelegate
Perhaps try:
也许尝试:
let app = UIApplication.sharedApplication()
EDIT (Swift 3):
编辑(斯威夫特 3):
It should be noted that in Swift 3, to accomplish this, you will want to access the shared
property instead:
应该注意的是,在 Swift 3 中,要完成此操作,您将需要访问该shared
属性:
let app = UIApplication.shared
回答by iPatel
You can do it by
你可以通过
let app = UIApplication.sharedApplication()