如何在Swift中获取应用程序代理的引用?
如何在Swift中获取应用程序代理的引用如何在Swift中获取应用程序代理的引用?,swift,Swift,如何在Swift中获取应用程序代理的引用 最后,我想使用引用来访问托管对象上下文。它与Objective-C中的内容基本相同 let del = UIApplication.sharedApplication().delegate 另一个解决方案是正确的,它将为您获取对应用程序委托的引用,但这将不允许您访问UIApplication子类添加的任何方法或变量,如托管对象上下文。要解决这个问题,只需向下转换到“AppDelegate”或您的UIApplication子类所调用的内容。在Swift
最后,我想使用引用来访问托管对象上下文。它与Objective-C中的内容基本相同
let del = UIApplication.sharedApplication().delegate
另一个解决方案是正确的,它将为您获取对应用程序委托的引用,但这将不允许您访问UIApplication子类添加的任何方法或变量,如托管对象上下文。要解决这个问题,只需向下转换到“AppDelegate”或您的UIApplication子类所调用的内容。在Swift 3、4和5中,操作如下:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let aVariable = appDelegate.someVariable
这可以用于OSX
let appDelegate = NSApplication.sharedApplication().delegate as AppDelegate
var managedObjectContext = appDelegate.managedObjectContext?
在Xcode 6.2中,这也适用
let appDelegate = UIApplication.sharedApplication().delegate! as AppDelegate
let aVariable = appDelegate.someVariable
方便构造函数
代码末尾的外接程序AppDelegate类
Swift 5
在类中使用AppDelegate引用吗
调用AppDelegate方法 appDelegate().setRoot() SWIFT<3 在AppDelegate类中为ex创建一个方法
func sharedInstance() -> AppDelegate{
return UIApplication.sharedApplication().delegate as! AppDelegate
}
把它叫做别的什么地方
let appDelegate : AppDelegate = AppDelegate().sharedInstance()
SWIFT>=3.0
Swift 4.2 使用Swift,在您的VC中轻松访问
extension UIViewController {
var appDelegate: AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
}
以下是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
在我的例子中,在我的
NSManagedObject
子类的顶部缺少import UIKit
。
导入后,我可以删除该错误,因为UIApplication
是UIKit
希望它能帮助别人 根据这里所说的,我错过了导入UIKit:
import UIKit
导入UIKit
让appDelegate=UIApplication.sharedApplication().delegate!作为!AppDelegate
我在Swift 2.3中使用它 1.1在AppDelegate类中
static let sharedInstance: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
2.使用以下命令调用AppDelegate
let appDelegate = AppDelegate.sharedInstance
在Swift 3.0中,您可以通过
let appDelegate = UIApplication.shared.delegate as! AppDelegate
以下是
uiapplicationelegate
的扩展,它避免了对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
试着简单地说:
Swift 4
// Call the method
(UIApplication.shared.delegate as? AppDelegate)?.whateverWillOccur()
在AppDelegate中的位置:
// MARK: - Whatever
func whateverWillOccur() {
// Your code here.
}
这很简单
应用程序代理实例
let app = UIApplication.shared.delegate as! AppDelegate
可以使用单行语法调用方法
app.callingMethod()
您可以使用此代码访问变量
app.yourVariable = "Assigning a value"
从iOS 12.2和Swift 5.0开始,
AppDelegate
不是可识别的符号<代码>UIApplicationLegate是。因此,任何涉及AppDelegate
的答案都不再正确。下面的答案是正确的,并且避免力展开,一些开发者认为代码气味:
import UIKit
extension UIViewController {
var appDelegate: UIApplicationDelegate {
guard let appDelegate = UIApplication.shared.delegate else {
fatalError("Could not determine appDelegate.")
}
return appDelegate
}
}
请注意,有些人会考虑使用App委托作为托管对象上下文的容器或其他“全局”对象是反模式。考虑让App委托将MOC传递给控制器,而不是让控制器找到它。@ KristopherJohnson Hey Kris,我如何方便AppDeRead注入依赖关系到视图控制器?以编程方式实例化视图控制器,并使用反射递归地决定实例化/注入什么?有什么框架可以帮助实现这一点吗?如果我必须手动为每个视图控制器执行此操作,我的AppDelegate将非常庞大!哦,最好不要为所有内容创建工厂:)一个粗略的搜索发现,它也有自动连接:)对于那些建议不要在app委托中添加任何“无关”内容的程序员来说,这有点荒谬,因为苹果的文档明确指出,
“关键角色”uiapplicationelegate
singleton的“…用于存储应用程序的中心数据对象或任何没有拥有视图控制器的内容。”
如果任何人仍有问题,针对OS X要求您导入Cocoa,以使其适用于NSApplication.sharedApplication()。我想iOS可能需要相同的答案。这是两个答案中更有用的一个。@zacjordaan使用该方法,您将获得属性的自动完成,但它实际上不起作用。这样,每次使用AppDelegate类时都会创建一个新的实例。最好使用可通过sharedApplication单例访问的委托。至于你的第二个问题,是的,你可能想用一个常数。即使您可能正在更改AppDelegate的属性,也可能不会将指针重新指定给其他任何对象,在这种情况下,不需要变量。这完全取决于你。做适合你需要的事。极好的建议!我曾错误地认为AppDelegate()是单例的,但在考虑了您所说的内容后,我意识到如果是单例的话,那么就不会有一个sharedApplication模式供我们以这种方式使用。当然,如果不需要的话,我不想产生不必要的实例,因此您的常量声明将非常适合;谢谢。我不想将此作为单独的答案添加,但要使用您自己的方法获取AppDelegate,在同时使用Swift和Obj-c的项目中的属性,您应该将#import“AppDelegate.h”添加到“ProjectName BridgingHeader.h”v01d中,从何处获得它?这同样有效,我喜欢sharedInstance命名的想法。谢谢您的示例仍然会在每次调用AppDelegate().sharedInstance()
时实例化一个新的AppDelegate
对象。我更喜欢这个解决方案,而不是公认的解决方案(如果您必须全面使用它,它会显得“不整洁”而冗长)。在找到这个答案之前,我一直在考虑为NSApplication
尝试一个扩展
,但这要好得多。现在你可能想使用一个类计算的只读属性shared
,但是,也许你想发布Swift 3的更新?我实际上正在重构我的代码以使用静态var shared:TournamentDelegate?{get{return NSApplication.shared().de
app.yourVariable = "Assigning a value"
extension AppDelegate {
// MARK: - App Delegate Ref
class func delegate() -> AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
}
import UIKit
extension UIViewController {
var appDelegate: UIApplicationDelegate {
guard let appDelegate = UIApplication.shared.delegate else {
fatalError("Could not determine appDelegate.")
}
return appDelegate
}
}