我試圖讓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。
這裏有什麼問題?
什麼,如果你完全刪除頭文件中的協議定義? –