In KMM application in shared module I have interface, let’s say it’s marker interface without any actual fields
interface Marker
In Kotlin I can use equals and hashCode on instances of that interface
fun isEqual(o1: Marker, o2: Marker): Boolean {
return o1 == o2
}
How I can do same thing in iosApp Swift code?
func isEqual(o1: shared.Marker, o2: shared.Marker): Bool {
// todo implement
}
I tried to invoke equals and hashCode methods on instances on Swift, but I got the error, that this methods don’t exist
>Solution :
Kotlin Native convert interfaces to Swift protocols. But methods equals, hashCode and toString are provided by kotlin.Any abstract class. These methods converts to NSObject’s method isEquals(_:) and the properties hash, description in Swift. Take a look at documentation for more details.
So basically you can implement what you want using cast to NSObject
func isEqual(o1: shared.Marker, o2: shared.Marker): Bool {
return (o1 as! NSObject).isEquals(o2 as! NSObject)
}