I'm defining a protocol in Swift language, and this protocol will be used in both Objective-C and Swift projects.
Its first version is as follows:
public protocol DataCodable {
func encode() -> Data?
func decode(from data: Data) -> Self?
}
Since it cannot be exported to Objective-C code, I have to rewrite it as:
@objc public protocol NSDataCodable {
func encode() -> NSData?
func decode(from data: NSData) -> Self?
}
Now I have two problems:
DataCodable
is only available in Swift.NSDataCodable
can only be applied to NSObject subclasses.
If I keep both protocols, I have to specify Any
to the data type I hold:
public class MyClass {
var data: Any? // DataCodable or NSDataCodable
}
Is there any best practices to solve problems like this?
I don't claim it to be any of the best practices, but...
I had a similar problem some years ago and ended up using two protocols and a wrapper for the Objective-C one: https://lazarevzubov.medium.com/compatible-with-objective-c-swift-code-e7c3239d949
Something like this:
MyClass.data
can have either a Swift implementation ofDataCodable
or an Objective-C implementation ofNSDataCodable
wrapped withNSDataCodableWrapper
.