我试图让Objective-C类采用一个用Swift文件编写的协议。我有Swift Objective-C在一定程度上互操作。 (我可以从Swift构建我的Objective-C类)。无法将符合协议的Objective-C类分配给使用Swift协议的属性
我:
@objc public protocol FooProtocol {
func foobar()
}
然后,我的Objective-C的文件:
#import <UIKit/UIKit.h>
#import "SwiftObjcProtocolTest-Bridging-Header.h"
@protocol FooProtocol
@end
@interface ObjClass1 : NSObject <FooProtocol>
-(void)foobar;
@end
并实现了一套:
#import "ObjClass1.h"
@implementation ObjClass1
- (void)foobar {
NSLog(@"foobar protocol function called");
}
@end
但是,当我给斯威夫特(主要是在应用程序这样做委托)委托属性并尝试将Objective-C对象分配给它:
var delegate: FooProtocol?
....
delegate = objcInstance
delegate?.foobar()
它失败: '?FooProtocol'
不能分配型 'ObjClass1' 的值输入。
我试过用as! FooProtocol
强迫它,但是这会导致SIGABRT。
这里有什么问题?
什么,如果你完全删除头文件中的协议定义? –