我在目标C项目中使用的是快捷盒。
我成功地从Objective-C访问swift类,无论如何我都面临采用swift协议的问题。
编译器不会抱怨,生成的桥接头似乎是正确的。
调用[self.sceneLocationView setLocationNodeTouchDelegate: self];
时会在运行时发生问题,导致 [ARCL.SceneLocationView setLocationNodeTouchDelegate:]:无法识别的选择器发送到实例。
我找不到解决此问题的任何方法,我花了一些时间在这里搜索,但仍然没有运气使它起作用。
任何帮助将不胜感激。
谢谢
我的swift和Objective-c文件如下所示。
SceneLocationView.swift:
@available(iOS 11.0, *)
open class SceneLocationView: ARSCNView {
...
@objc public weak var locationNodeTouchDelegate: LNTouchDelegate?
...
}
SceneLocationViewDelegate.swift:
@objc public protocol LNTouchDelegate: class {
func locationNodeTouched(node: AnnotationNode)
}
ViewController.h:
#import "ARCL-Swift.h"
@interface ViewController : UIViewController <LNTouchDelegate>{
}
@property (nonatomic, strong) SceneLocationView *sceneLocationView;
ViewController.m:
#import "ARCL-Swift.h"
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.sceneLocationView = [[SceneLocationView alloc] init];
[self.sceneLocationView run];
[self.view addSubview: self.sceneLocationView];
...
[self.sceneLocationView setLocationNodeTouchDelegate: self]; <-- ERROR HERE
}
-(void) locationNodeTouchedWithNode:(AnnotationNode *)node {
}
ARCL-Swift.h:
SWIFT_PROTOCOL("_TtP4ARCL15LNTouchDelegate_")
@protocol LNTouchDelegate
- (void)locationNodeTouchedWithNode:(AnnotationNode * _Nonnull)node;
@end
SWIFT_CLASS("_TtC4ARCL17SceneLocationView") SWIFT_AVAILABILITY(ios,introduced=11.0)
@interface SceneLocationView : ARSCNView
@property (nonatomic, weak) id <LNTouchDelegate> _Nullable locationNodeTouchDelegate;
- (nonnull instancetype)init;
- (nonnull instancetype)initWithFrame:(CGRect)frame options:(NSDictionary<NSString *, id> * _Nullable)options OBJC_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder * _Nonnull)aDecoder OBJC_DESIGNATED_INITIALIZER;
- (void)layoutSubviews;
- (nonnull instancetype)initWithFrame:(CGRect)frame SWIFT_UNAVAILABLE;
@end