Just trying to get started with Swift and hit the following issue when upgrading to Swift 1.2:
@protocol MyObjcProtocol <NSObject>
@optional
@property (copy) NSString *optionalString;
- (void) optionalMethod;
@end
...
class MySwiftClass: NSObject {}
extension MySwiftClass: MyObjcProtocol {
var optionalString: NSString {
get { return "Foo" }
set(newValue) { NSLog("Whatever") }
}
// No problem here
func optionalMethod() {
NSLog("Bar")
}
}
The Swift extension implementing the Objc protocol doesn't compile with:
Objective-C method 'optionalString' provided by getter for 'optionalString' conflicts with optional requirement getter for 'optionalString' in protocol 'MyObjcProtocol'...
Objective-C method 'setOptionalString:' provided by setter for 'optionalString' conflicts with optional requirement setter for 'optionalString' in protocol 'MyObjcProtocol'...
So clearly the compiler doesn't realise I'm trying to implement the optionals from the protocol, and thinks I'm stomping on the protocol's expected ObjC symbols. The optional method func optionalMethod()
compiles just fine, however. Remove @optional
from the protocol and everything compiles just fine, but it's not always possible or desirable to do that as a solution.
So, how does one implement this? Trying to implement the expected ObjC methods explicitly doesn't work either:
func optionalString() {
return "foo"
}
func setOptionalString(newValue: NSString) {
NSLog("")
}
Hope someone can help! Thanks in advance!