I'm trying to have an Objective-C class adopt a protocol written in a Swift file. I've got Swift an Objective-C inter-operating to some extent. (I can construct my Objective-C class from Swift).
I have:
@objc public protocol FooProtocol {
func foobar()
}
Then, my Objective-C file:
#import <UIKit/UIKit.h>
#import "SwiftObjcProtocolTest-Bridging-Header.h"
@protocol FooProtocol
@end
@interface ObjClass1 : NSObject <FooProtocol>
-(void)foobar;
@end
and impl:
#import "ObjClass1.h"
@implementation ObjClass1
- (void)foobar {
NSLog(@"foobar protocol function called");
}
@end
But when I give Swift (doing this mostly in app delegate) a delegate property and try to assign the Objective-C object to it:
var delegate: FooProtocol?
....
delegate = objcInstance
delegate?.foobar()
it fails with:
cannot assign value of type 'ObjClass1' to type 'FooProtocol?'.
I've tried coercing it with as! FooProtocol
but this results in a SIGABRT.
What is the issue here?