首先我知道这一点:如何获取 Swift 中应用程序委托的引用? https://stackoverflow.com/questions/24046164/how-do-i-get-a-reference-to-the-app-delegate-in-swift
其次,我需要做的是访问混合应用程序 Swift 端的 appdelegate 属性。
基本上,
1-我有一个项目是作为 Objective C 项目启动的。这意味着 AppDelegate 是在 Objective C 端定义的。
2-我的快速代码工作正常,我有一个桥头,我可以从另一侧的任何一侧引用事物。
3- 问题是这样的:要在我的 Swift 代码中引用 appdelegate,我需要#import "AppDelegate.h"
在我的桥接标头中。但由于其他原因,我还需要 AppDelegate.h 来导入 SWIFT 标头(PROJECT-Swift.h
)。这将创建一个参考循环。
有没有办法避免这种参考循环?并仍然访问 AppDelegate 属性?
编辑:我在问题的第一版中没有提到的另一个复杂之处是,我想要向 Swift 代码公开的 AppDelegate 属性实际上是在 Swift 端声明的类型。所以我需要在AppDelegate.h
为了能够做到这一点,我需要导入-Swift.h
我的标题AppDelegate.h
.
为了更清楚地说明:
KB
is a public class
在 Swift 端定义。
AppDelegate 具有如下属性:@property (strong) KB *appkb;
我需要抓住((AppDelegate*)UIApplication.SharedApplication()).appkb
你应该导入PROJECT-Swift.h
in AppDelegate.m
, not .h
In AppDelegate.h
,您可以使用“前向声明”(@class
and @protocol
) like:
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:
#import "AppDelegate.h"
#import "PROJECT-Swift.h"
@implemetation AppDelegate
//....
@end
项目-桥接-Header.h
#import "AppDelegate.h"
任何.swift:
@objc public protocol SwiftProtocol {
// ...
}
@objc public class SwiftClass:NSObject {
// ...
}
@objc public class KB:NSObject {
// ...
}
该文件 https://developer.apple.com/library/ios/documentation/swift/conceptual/buildingcocoaapps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_86 says:
为了避免循环引用,请勿将 Swift 导入 Objective-C 头文件。相反,您可以转发声明一个 Swift 类以在 Objective-C 标头中使用它。请注意,您不能在 Objective-C 中子类化 Swift 类。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)