https://kotlinlang.org logo
Title
a

adev_one

05/19/2018, 12:24 AM
When I'm trying to implement kotlin interface in swift my app crashes:
Unrecognized selector -[ios.IosApiRequestsMaker toKotlin:]
2018-05-19 05:15:48.277185+0500 ios[47861:1486398] Unrecognized selector -[ios.IosApiRequestsMaker toKotlin:]
Is there way to fix it?
o

olonho

05/19/2018, 6:59 AM
to fix this issue (as most others), we need more context, i.e. a minimal reproducer, code snippet etc., we cannot fix problem just from reading fragment of the error message
a

adev_one

05/19/2018, 8:16 AM
Kotlin `interaface`:
interface ApiRequestsMaker {

    interface Callback {
        fun onResponse(httpCodeAndResponse: Pair<Int, String>)
        fun onError(e: Throwable)
    }

    fun <TEntity, TResult : Any, TParams : Any> make(
        request: ApiRequest<TEntity, TResult, TParams>,
        params: TParams,
        callback: Callback
    )

}
Swift implementation of `protocol`:
class IosApiRequestsMaker: NFApiRequestsMaker {
    
    func make(request: NFApiRequest, params: Any, callback: NFApiRequestsMakerCallback) {
        print(123)
        callback.onResponse(httpCodeAndResponse: NFStdlibPair(first: Int32(200), second: ""))
    }
    
}
`build.gradle`:
buildscript {
    ext.kotlin_version = '1.2.41'
    repositories {
        jcenter()
        maven {
            url "<https://dl.bintray.com/jetbrains/kotlin-native-dependencies>"
        }
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:0.7"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

apply plugin: 'konan'

konan.targets = [
        'ios_arm64', 'ios_x64'
]

konanArtifacts {
    framework('NF') {
        enableMultiplatform true
    }
}

dependencies {
    <...>
}
from stacktrace:
ios`NFApiRequestsHandler.__allocating_init(apiRequestsMaker:exceptionsProvider:connectionChecker:):
    0x104f714b0 <+0>:  pushq  %rbp
    0x104f714b1 <+1>:  movq   %rsp, %rbp
    0x104f714b4 <+4>:  pushq  %r13
    0x104f714b6 <+6>:  subq   $0x18, %rsp
    0x104f714ba <+10>: movq   %rdi, -0x10(%rbp)
    0x104f714be <+14>: movq   %r13, %rdi
    0x104f714c1 <+17>: movq   %rsi, -0x18(%rbp)
    0x104f714c5 <+21>: movq   %rdx, -0x20(%rbp)
    0x104f714c9 <+25>: callq  0x104f74076               ; symbol stub for: swift_getObjCClassFromMetadata
    0x104f714ce <+30>: movq   %rax, %rdi
    0x104f714d1 <+33>: callq  0x104f74016               ; symbol stub for: objc_allocWithZone
    0x104f714d6 <+38>: movq   -0x10(%rbp), %rdi
    0x104f714da <+42>: movq   -0x18(%rbp), %rsi
    0x104f714de <+46>: movq   -0x20(%rbp), %rdx
    0x104f714e2 <+50>: movq   %rax, %r13
    0x104f714e5 <+53>: callq  0x104f73730               ; @nonobjc __ObjC.NFApiRequestsHandler.init(apiRequestsMaker: __ObjC.NFApiRequestsMaker, exceptionsProvider: __ObjC.NFApiExceptionsProvider, connectionChecker: __ObjC.NFConnectionChecker) -> __ObjC.NFApiRequestsHandler at FeedViewController.swift
0x104f714ea <+58>: addq   $0x18, %rsp
0x104f714ee <+62>: popq   %r13
    0x104f714f0 <+64>: popq   %rbp
    0x104f714f1 <+65>: retq
Enviroment: mac os x 10.13.4, latest iOS simulator
y

yusuf3000

05/19/2018, 9:22 AM
Mark
IosApiRequestsMaker
with
@objc
. Your class then will then also need to subclass
NSObject
@objc
class IosApiRequestsMaker: NSObject, NFApiRequestsMaker {
    
    func make(request: NFApiRequest, params: Any, callback: NFApiRequestsMakerCallback) {
        print(123)
        callback.onResponse(httpCodeAndResponse: NFStdlibPair(first: Int32(200), second: ""))
    }
    
}
r

russhwolf

05/19/2018, 12:28 PM
I think just extending NSObject will be sufficient, unless something changed between 0.6 and 0.7. See here: https://stackoverflow.com/questions/49092085/error-when-passing-a-swift-implementation-of-a-kotlin-interface-to-a-kotlin-meth
a

adev_one

05/19/2018, 3:19 PM
Extending NSObject works. Thanks!