WatchKit中的UIApplication.sharedApplication().delegate等效于什么? [英] What is the equivalent of UIApplication.sharedApplication().delegate in WatchKit?

查看:105
本文介绍了WatchKit中的UIApplication.sharedApplication().delegate等效于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS应用程序中,您可以通过以下方式获取对共享应用程序委托的引用:

In an iOS application, you can obtain a reference to the shared app delegate by:

快捷键:
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate

Objective-C:
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];

在WatchKit2应用程序扩展中,有一个类似的应用程序委托,我想在视图控制器中获取对它的引用以访问应用程序中的共享资源,例如我拥有的Cored堆栈的ManagedObjectModel和PersistentStoreCoordinator在应用程序委托中初始化.

In a WatchKit2 App Extension there is a similar App Delegate and I want to obtain a reference to it in a view controller to access shared resources in the app, such as the ManagedObjectModel and PersistentStoreCoordinator for the Core Data stack, that I have initialised in the App Delegate.

但是,UIApplication.sharedApplication().delegate as! AppDelegate报告错误,

使用未解析的标识符'UIApplication'

Use of unresolved identifier 'UIApplication'

如何在WatchKit2 App Extension中访问应用程序委托?

How can I access the app delegate in a WatchKit2 App Extension?

推荐答案

WatchOS 2中的WKExtension类自动为每个扩展提供一个扩展对象,以管理在您所有应用的界面控制器之间共享的行为. Apple文档指出您使用了扩展对象,以执行应用程序级任务,例如打开URL和获取应用程序的根接口控制器."

The WKExtension class in WatchOS 2 automatically provides a single extension object for each extension, to manage behaviors that are shared among all of your app’s interface controllers. The Apple Documentation notes that you "use the extension object to perform app-level tasks such as opening URLs and getting the root interface controller of your app."

就像在iOS中一样,在WatchKit App Extension中,您将提供自己的委托对象,该对象是您要引用的对象.这会自动分配给WKExtension对象的委托属性,并且可以使用与访问iOS中的UIApplication委托所使用的方法类似的方法进行访问:

Just like in iOS, in your WatchKit App Extension you are providing your own delegate object, the one you are trying to reference. This is automatically assigned to the delegate property of the WKExtension object, and can be accessed using a similar method to that used to access the UIApplication delegate in iOS:

快捷键:
let delegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate

Objective-C:
WKExtensionDelegate *delegate = [[WKExtension sharedExtension] delegate];

WKExtension类的Apple文档提供有关功能的更多信息.

The Apple documentation of the WKExtension Class provides further information about capabilities.

更深入:
WatchKit App Extensions并非在所有情况下都必须提供WKExtensionDelegate.作为 WKExtensionDelegate文档指出:您可以提供委托对象,并使用它来管理扩展中的生命周期事件.如果扩展支持可操作的通知或切换行为,则需要提供委托对象."

Going deeper:
WatchKit App Extensions do not under all circumstances have to provide a WKExtensionDelegate. As the WKExtensionDelegate documentation from Apple notes, "You provide the delegate object and use it to manage lifecycle events in your extension. Providing a delegate object is required if your extension supports actionable notifications or Handoff behaviours."

您将知道WatchKit App Extension是否具有委托对象,如果有,则您在App Extension生命周期中没有尝试访问不存在的App Delegate的意义.因此,尽管WKExtension.sharedExtension().delegate是可选的(在未设置委托的情况下可能存在WatchKit App Extensions),但在开发人员已知的情况下,使用as!强制将返回值强制转换为非可选是安全的.他们在自己的应用扩展程序中设置了WKExtensionDelegate.

You will know whether your WatchKit App Extension has a delegate object, and if it does, there will be no point in your App Extension lifecycle where you are trying to access that App Delegate where it will not exist. Therefore, while the WKExtension.sharedExtension().delegate is an optional (WatchKit App Extensions could exist where no delegate has been set) it is safe to use as! to force cast the return to be non-optional in the example above, given the developer knows they have set a WKExtensionDelegate in their App Extension.

这篇关于WatchKit中的UIApplication.sharedApplication().delegate等效于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆