If I have a Kotlin interface with functions or var...
# ios
d
If I have a Kotlin interface with functions or variables that have a body and which I don't want to override, when implementing it in Swift, it requires me to still override everything.
For example:
Copy code
interface MetaWearDevice {

    companion object {
        private val _connectionState = MutableStateFlow(false)
    }

    val connectionState: Flow<Boolean> get() = _connectionState
    val isConnected: Boolean get() = _connectionState.value

    // This notation effectively makes the function protected
    /** Needs to be called inside connect and disconnect functions */
    fun MetaWearDevice.changeConnectionState(state: Boolean) {
        _connectionState.value = state
    }

    /** This function should also register a callback that sets the connection state to false when the device disconnects */
    suspend fun connect()
Have to override or it doesn't conform to the protocol
Copy code
class MetaWearDeviceImpl : MetaWearDevice {
    var connectionState: any Kotlinx_coroutines_coreFlow
    
    var isConnected: Bool
    
    func changeConnectionState(_ receiver: any MetaWearDevice, state: Bool) {
        <#code#>
    }
hmm as I workaround, I can create those variables as extensions and now Swift doesn't need me to override them.
Copy code
val MetaWearDevice.isConnected: Boolean get() = this.connectionState.value

/** Needs to be called inside connect and disconnect functions */
fun MetaWearDevice.changeConnectionState(state: Boolean) {
    this.connectionState.value = state
}
But idealy I would still like to define them inside the interface and not have swift demand a seperate implementation
f
abstract class doestn’t exist in swift. So you found the only way to use it 🙂