Please Help Me
Please Help Me

Reputation: 73

Kotlin Multi Platform, Access Swift Standard Library

I have an actual class that I want to implement swift Hashable but I can't find the import for swift Hashable inside of Kotlin.

If I wanted to implement say UIView I could import it easily and use it as follows:

import platform.UIKit.UIView

actual final class Example actual constructor() : UIView(.... Stuff here)  {
    
}

But what about Hashable? and other things from the swift standard library? I can't find them inside platfrom, I have also looked inside platfrom.objc, but can't find the standard lib anywhere.

Upvotes: 2

Views: 503

Answers (1)

Phil Dukhov
Phil Dukhov

Reputation: 88347

Hashable is defined in Swift, KMP doesn't support Swift interop at the moment. In Kotlin you can only use code that's available from ObjC.

But all NSObject subclasses are Hashable by default, so any object created from KMP will also be hashable.

Note that in the case of NSObject, the hash is based on the object pointer, not on object properties, as you may be used to in Swift. You can override this logic from Swift:

extension KObject {
    public override var hash: Int {
        // your logic
    }
}

or from Kotlin implementation:

override fun hash(): NSUInteger {
    // your logic
}

Upvotes: 2

Related Questions