如何在混合应用程序(主要语言 ObjC)中从 Swift 获取对 appdelegate 的引用以避免引用循环 [英] Ho to get a reference to appdelegate from Swift in a mixed application (primary language ObjC) to avoid reference loop

查看:24
本文介绍了如何在混合应用程序(主要语言 ObjC)中从 Swift 获取对 appdelegate 的引用以避免引用循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我知道这个:如何在 Swift 中获得对应用程序委托的引用?

其次,我需要做的是访问混合应用程序的 Swift 端的 appdelegate 属性.

Second, what I need to do is to access the appdelegate properties in the Swift side of a mixed app.

基本上,
1- 我有一个作为 Objective C 项目启动的项目.这意味着 AppDelegate 是在 Objective C 端定义的.
2- 我的 swift 代码工作正常,我有一个桥头,我可以从另一边的任何一边引用东西.
3- 问题如下:要在我的 Swift 代码中引用 appdelegate,我需要在我的桥接头中 #import "AppDelegate.h".但由于其他原因,我还需要 AppDelegate.h 来导入 Swift 标头 ( PROJECT-Swift.h ).这将创建一个引用循环.

Basically,
1- I have a project which is started as an Objective C project. This means AppDelegate is defined in the Objective C side.
2- I have swift code working fine, I have a bridge header and I can reference things form either side on the other side.
3- Here is the problem: To reference the appdelegate in my Swift code I need to #import "AppDelegate.h" in my bridging header. But for other reasons, I also need the AppDelegate.h to import the SWift header ( PROJECT-Swift.h ). This creates a reference loop.

有没有办法避免这种引用循环?仍然可以访问 AppDelegate 属性?

Is there a way to avoid this reference loop? and still access AppDelegate properties?

我在问题的第一版中没有提到的另一个复杂问题是,我想向 Swift 代码公开的 AppDelegate 属性实际上是在 Swift 端声明的类型.所以我需要在 AppDelegate.h 中声明它,为了能够做到这一点,我需要在我的 AppDelegate 中导入 -Swift.h 标头.h.
为了更清楚:
KB 是在 Swift 端定义的 public class.
AppDelegate 有一个类似的属性:@property (strong) KB *appkb;我需要掌握 ((AppDelegate*)UIApplication.SharedApplication()).appkb

An additional complication that I did not mention in the first edition of the question is that, the AppDelegate property that I want to expose to Swift code, is actually of a Type declared in the Swift side. So I need to declare it in the AppDelegate.h and to be able to do that, I need to import the -Swift.h header in my AppDelegate.h.
To make it more clear:
KB is a public class defined on the Swift side.
AppDelegate has a property like: @property (strong) KB *appkb; I need to get a hold of ((AppDelegate*)UIApplication.SharedApplication()).appkb

推荐答案

你应该在 AppDelegate.m 中导入 PROJECT-Swift.h,而不是 .h

You should import PROJECT-Swift.h in AppDelegate.m, not .h

AppDelegate.h中,您可以使用前向声明"(@class@protocol),例如:

In AppDelegate.h, you can use "forward declaration" (@class and @protocol) like:

AppDelegate.h:

AppDelegate.h:

#import <UIKit/UIKit.h>

@class SwiftClass;
@class KB;
@protocol SwiftProtocol;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) id<SwiftProtocol> swifter;
@property (strong, nonatomic) KB *appkb;

-(void)methodReceiveSwiftClass:(SwiftClass *)obj;

//...

@end

AppDelegate.m:

AppDelegate.m:

#import "AppDelegate.h"
#import "PROJECT-Swift.h"

@implemetation AppDelegate

//....

@end

PROJECT-Bridging-Header.h

PROJECT-Bridging-Header.h

#import "AppDelegate.h"

Any.swift:

@objc public protocol SwiftProtocol {
    // ...
}

@objc public class SwiftClass:NSObject {
    // ...
}

@objc public class KB:NSObject {
    // ...
}

文档 说:

为了避免循环引用,不要将 Swift 导入到 Objective-C 头文件中.相反,您可以转发声明一个 Swift 类以在 Objective-C 标头中使用它.请注意,您不能在 Objective-C 中对 Swift 类进行子类化.

To avoid cyclical references, don’t import Swift into an Objective-C header file. Instead, you can forward declare a Swift class to use it in an Objective-C header. Note that you cannot subclass a Swift class in Objective-C.

这篇关于如何在混合应用程序(主要语言 ObjC)中从 Swift 获取对 appdelegate 的引用以避免引用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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