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

问题描述

首先,我对此有所了解:

First of all I know about this: How do I get a reference to the app delegate in 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-我的快速代码工作正常,我有一个桥头,可以在另一侧的任何一侧引用事物.
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端声明的Type.因此,我需要在AppDelegate.h中声明它并能够做到这一点,我需要在我的AppDelegate.h中导入-Swift.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:

Any.swift:

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

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

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

文档说:

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

11-03 03:43