Hey, I try to convert the following swift code to ...
# kotlin-native
t
Hey, I try to convert the following swift code to Kotlin. It compiles perfectly, but I get a Nullpointer error at runtime: Swift:
Copy code
private var readStream: Unmanaged<CFReadStream>?
private var writeStream: Unmanaged<CFWriteStream>?
private var inputStream: InputStream?
private var outputStream: OutputStream?

func connect(
        hostname: String,
        port: Int32,
        callback: @escaping (_ dataTransferObject: DiagnoseViewDataTransferObject) -> Void
 ) {
        let host = CFStringCreateWithCString(kCFAllocatorDefault, hostname, CFStringBuiltInEncodings.UTF8.rawValue)
        CFStreamCreatePairWithSocketToHost(
            kCFAllocatorDefault,
            host,
            443,
            &self.readStream,
            &self.writeStream
        )

	inputStream = readStream?.takeRetainedValue()
       outputStream = writeStream?.takeRetainedValue()
       inputStream!.delegate = self
       outputStream!.delegate = self
    }
---------------- In Kotlin I have the following:
Copy code
private var readStream: CValuesRef<CFReadStreamRefVar>? = null
private var writeStream: CValuesRef<CFWriteStreamRefVar>? = null
private var inputStream: NSInputStream? = null
private var outputStream: NSOutputStream? = null

fun connect(
        hostname: String,
        port: Int,
        callback: (dataTransferObject: DiagnoseViewDataTransferObject) -> Unit
 ) {
        val host = CFStringCreateWithCString(kCFAllocatorDefault, hostname, kCFStringEncodingUTF8)
        CFStreamCreatePairWithSocketToHost(
            alloc = kCFAllocatorDefault,
            host = host,
            port = 443,
            readStream = this.readStream,
            writeStream = this.writeStream
         )  
        inputStream = readStream as NSInputStream?
        outputStream = writeStream as NSOutputStream?
        inputStream!!.delegate = this //crashes here with Nullpointer
        outputStream!!.delegate = this

}
Someone can help me how to use CFStreamCreatePairWithSocketToHost from Kotlin?
m
You can wrap your code in triple backticks (```) to create a code block.
t
Thx. Fixed it 😄
r
You never assign
readStream
so it's always
null
. So you only ever set
inputStream
to
null
. Also your types don't match, so I don't think the
as NSInputStream?
cast will never succeed unless it's a usage of
CValuesRef
I don't know about (which is possible because I haven't done a ton with K/N value references)
t
Hey, in Swift 'CFStreamCreatePairWithSocketToHost' works this way: I pass the uninitialized references to
CFReadStream
and
CFWriteStream
. The call to 'CFStreamCreatePairWithSocketToHost' initializes them. This works in Swift, but not in Kotlin. Presumably I use the wrong types? The cast does not give me the warning "This cast can never succeed". But maybe it's wrong too.
a
Hello! I’d recommend to allocate this
CFReadStreamRef
and send pointer to the function rather than create a CValuesRef as you done. Also I agree, that these cast shouldn’t work as-is, one got to manage with bridging as described in the Apple documentation(https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/tollFreeBridgedTypes.html)