For instance, I want to extract a Swift protocol from my existing Objective-C class MyFoo
. Let's call this protocol FooProtocol
.
The situation looks like this:
// In Objective-C
@interface MyFoo
@property(nonatomic, copy) NSString *foo;
@end
@implementation MyFoo
// ... -(instancetype)initWithString: is implemented here
@end
// In Swift
@objc protocol FooProtocol {
var foo: String { get set }
}
extension MyFoo: FooProtocol {
// do nothing here!
}
Then I should be allowed to do this:
let theFoo: FooProtocol = MyFoo(string: "Awesome")
NSLog("\(theFoo.foo)") // Prints awesome.
But I get told "MyFoo does not conform to protocol FooProtocol". Okay. Fair enough, I guess the protocol extension needs a little nudge:
extension MyFoo: FooProtocol {
var foo: String! { get { return foo } set { NSLog("noop") }}
}
but I'm getting errors from the compiler that look like
Getter for 'foo' with Objective-C selector 'foo' conflicts with previous declaration with the same Objective-C selector
What am I doing wrong?