Sam
03/29/2018, 4:54 AMAccount
defined in Swift as a subclass of NSObject. I'm trying to pass it into a method of a class in a framework. However to pass that object to some of the foundation classes it needs to be of type NSObject or the compiler will complain. Any cast I've tried has failed at runtime with an exception for me. If I take out the if check it will give a class cast exception
override fun put(obj: T): UUID {
if (obj !is ObjCObject) { //Tried NSObject, NSObjectProtocol
throw IllegalArgumentException("Object: $obj must be a subclass of NSObject.")
}
val objc = obj as ObjCObject
val data = NSKeyedArchiver.archivedDataWithRootObject(objc) // Won't take obj so need to cast.
...
}
//Swift class
class Account: NSObject {
let name: String
init(name: String) {
self.name = name
}
}
svyatoslav.scherbina
03/29/2018, 7:40 AMSam
03/29/2018, 11:15 AMolonho
03/29/2018, 11:47 AMSam
03/29/2018, 1:44 PMsvyatoslav.scherbina
03/29/2018, 2:37 PMLong
instead of object. This Long
value would represent raw pointer to object. Then wrap this value manually before passing to foundation classes.
Kotlin:
import kotlinx.cinterop.*
...
override fun put(objPtr: Long) {
val obj = interpretObjCPointer<ObjCObject>(nativeNullPtr + objPtr)
val data = NSKeyedArchiver.archivedDataWithRootObject(obj)
}
Swift:
put(unsafeBitCast(obj, to: Int64.self))
(where obj
is of Objective-C type)Sam
03/29/2018, 3:04 PMSam
03/30/2018, 4:43 AMsvyatoslav.scherbina
03/30/2018, 7:21 AMplatform.*.*
APIs from Kotlin code.
(the same for passing objects back to Objective-C world).
That’s why Kotlin objects formed by 1) can’t be passed directly to 2).
We are going to improve the situation soon.svyatoslav.scherbina
04/04/2018, 7:02 AMSam
04/04/2018, 11:30 AM