I am unable to get a really basic selector working...
# kotlin-native
j
I am unable to get a really basic selector working in iOS - here's what I'm doing.
Copy code
class ClosureSleeve(val closure: () -> Unit) : NSObject() {

    @ObjCAction
    fun runContainedClosure() {
        closure()
    }

    val selector: COpaquePointer? get() = Companion.selector

    @ThreadLocal
    companion object {
        var selector: COpaquePointer? = sel_registerName("runContainedClosure")

        fun printMethods(){
            val instance = ClosureSleeve{}
            val type = object_getClass(instance)

            println("Now there are implementations:")
            memScoped {
                val sizeOutput = this.alloc<UIntVar>()
                println("My class: ${type}")
                val mlist = class_copyMethodList(type, sizeOutput.ptr)
                if(mlist == null){
                    println("Failed to pull selectors, null response.")
                    return@memScoped
                }
                val size = sizeOutput.value.toInt()
                println("Selector size: $size")
                for (i in 0 until size) {
                    println("Method $i: ${sel_getName(method_getName(mlist[i]))?.toKString()}")
                }
            }
            println("Attempted registry: $selector")
            println("End selectors.")
        }
    }
}
I'm then using it from Swift to keep that section isolated:
Copy code
let sleeve = ClosureSleeveKt.makeSleeve {
            print("Test")
        }
        print("Sleeve: \(sleeve)")
        self.sleeve = sleeve //Not to worry; strong ref here
        button.addTarget(sleeve, action: NSSelectorFromString("runContainedClosure"), for: .touchUpInside)
When it prints out what methods are available,
runContainedClosure
is not there, just
init
and
dealloc
. Can anybody help? I've been working on this for several days and been unable to get it to work. I see that @spierce7 successfully got something like it working at one point.
For anyone wondering, I just tested this in a separate project. Don't retain a selector in Kotlin and things seem fine. I think there's more to be done, and I'll record more stuff here for anyone looking at this down the road.
a
Hello! Thank you for such a detailed report! This issue is fixed on the current
master
branch, so it has to be gone on the next release. For now, I can recommend to follow this mate’s steps compile the compiler from some of the recent
-dev-
releases(like this one https://github.com/JetBrains/kotlin-native/releases/tag/build-1.3.50-dev-2041)
j
@Artyom Degtyarev [JB] awesome! Thank you guys!
l
Are you sure it's fixed? If a module has a class that has the
@ExportObjCClass
annotation and is being targeted by a selector (
NSSelectorFromString
or
sel_registerName
with
@ObjCAction
on the public function), it still doesn't work unless the module is exported in the consumer project. That forces exposing implementation details into the configuration of the host projects, which can be quite confusing for libraries that have multiple modules.