我如何在Swift中获得对AppDelegate的引用?
最终,我想使用该引用来访问托管对象上下文。
我如何在Swift中获得对AppDelegate的引用?
最终,我想使用该引用来访问托管对象上下文。
另一种解决方案是正确的,它将为您提供对应用程序委托的引用,但这不允许您访问由UIApplication的子类添加的任何方法或变量,比如您的托管对象上下文。要解决这个问题,只需将其向下转换为"AppDelegate"或您的UIApplication子类名称。在Swift 3、4和5中,操作如下:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let aVariable = appDelegate.someVariable
Swift 4.2
在 Swift 中,可以轻松访问您的 VC。
extension UIViewController {
var appDelegate: AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
}
将以下代码添加到AppDelegate类的末尾
func appDelegate() -> AppDelegate {
guard let delegate = UIApplication.shared.delegate as? AppDelegate else {
fatalError("could not get app delegate ")
}
return delegate
}
func sceneDelegate() -> SceneDelegate {
guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else {
fatalError("could not get scene delegate ")
}
return sceneDelegate
}
这可以用于OS X操作系统
let appDelegate = NSApplication.sharedApplication().delegate as AppDelegate
var managedObjectContext = appDelegate.managedObjectContext?
let appDelegate = NSApp.delegate as AppDelegate
的意思是将NSApp的代理转换为AppDelegate类型,并将其赋给appDelegate变量。 - Vladimir Prudnikovlet appDelegate = NSApplication.shared().delegate as AppDelegate
。 - DirkNSApp.delegate as? AppDelegate
的意思是将 NSApp.delegate
转换为类型 AppDelegate
,并使用可选项来处理可能的类型转换错误。 - NickNSApplication.shared.delegate as? AppDelegate
更新至 Swift 5* - Roshan Sah它基本上和Objective-C中的一样
let del = UIApplication.sharedApplication().delegate
这里是 Swift 5 版本:
let delegate = UIApplication.shared.delegate as? AppDelegate
要访问受管理的对象上下文:
if let delegate = UIApplication.shared.delegate as? AppDelegate {
let moc = delegate.managedObjectContext
// your code here
}
或者,使用 guard:
guard let delegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let moc = delegate.managedObjectContext
// your code here
除此之外,就我个人而言,我错过了导入UIKit:
import UIKit
无法在作用域中找到“UIApplication”
。 - Marlhex在AppDelegate类中创建一个方法,例如:
func sharedInstance() -> AppDelegate{
return UIApplication.sharedApplication().delegate as! AppDelegate
}
并在其他地方调用它,例如
let appDelegate : AppDelegate = AppDelegate().sharedInstance()
func sharedInstance() -> AppDelegate{
return UIApplication.shared.delegate as! AppDelegate
}
AppDelegate().sharedInstance()
,您的示例仍会实例化一个新的 AppDelegate
对象。 - pxpgraphicsNSApplication
编写一个extension
,但这个方法要好得多。现在,你可能想要使用一个类计算只读属性shared
,也许你想发布一个Swift 3的更新? - Patrustatic var shared:TournamentDelegate?{get {return NSApplication.shared()。delegate as?TournamentDelegate}}
,这更符合开发Swift 3风格,使用计算只读属性来处理此类事情。一旦重构完成,我可能能够删除?
,你觉得呢?(抱歉,在评论中的代码格式总是很混乱。) - Patru请尝试以下方法:
Swift 4
// Call the method
(UIApplication.shared.delegate as? AppDelegate)?.whateverWillOccur()
在你的AppDelegate中:
// MARK: - Whatever
func whateverWillOccur() {
// Your code here.
}
这里有一个针对 UIApplicationDelegate
的扩展,可以避免直接硬编码 AppDelegate
类名:
extension UIApplicationDelegate {
static var shared: Self {
return UIApplication.shared.delegate! as! Self
}
}
// use like this:
let appDelegate = MyAppDelegate.shared // will be of type MyAppDelegate
NSApplicationDelegate
时,会出现以下提示:'Self' is only available in a protocol or as the result of a method in a class; did you mean 'AppDelegate'?
。这是过时的吗? - pkambUIApplicationDelegate
替换为 NSApplicationDelegate
,UIApplication
替换为 NSApplication
。 - nyg
UIApplicationDelegate
单例的一个“关键作用”是“存储您的应用程序的中央数据对象或任何没有拥有视图控制器的内容。” https://developer.apple.com/documentation/uikit/uiapplicationdelegate - trndjc