josephivie
07/08/2019, 8:33 PMclass 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:
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.josephivie
07/09/2019, 5:21 PMArtyom Degtyarev [JB]
07/10/2019, 12:26 PMmaster
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)joseph_ivie
07/10/2019, 3:59 PMlouiscad
05/11/2023, 11:21 AM@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.