I want to covert the corresponding swift method into kotlin for kmm
func checkIfDirectoryExist(fileURL: URL) -> Bool {
var isDir: ObjCBool = false
if FileManager.default.fileExists(atPath: fileURL.path, isDirectory: &isDir)
{
if isDir.boolValue {
return true
} else {
return false
}
}
return false
}
However I am not sure hot to pass Boolean pointer in kotlin as there are no toCPointer<Boolean>() method neither ** works
Working with Objective-C APIs in Kotlin/Native can be complicated. In many cases such as this one, you're actually interacting with elements of C such as pointers and memory allocation.
Understanding memory use for the
isDirectoryboolean in SwiftGiven the Swift code that you wrote:
If we consider how this code is running it might help to understand how to write this in Kotlin/Native:
isDirvariable is defined as anObjCBooltype.isDirto thefileExists()method, an&is used in order to pass the variable by reference (read/write), not by value (read only).fileExists()method not only returns the boolean indicator of whether the file exists or not, but it also populates the value of theisDirvariable with the result (often in Swift referred to asin/out).isDirectoryargument is a pointer to a boolean (BOOL *), as opposed to the return value of the method which is just a boolean (BOOL).Allocating memory for
isDirin K/N usingmemScopedandallocIn Kotlin/Native, we need to allocate memory for the
isDirvariable to be populated, which relies on leveraging thekotlinx.cinteropsuite of methods.Using
memscopedwill allocate memory for use within the block passed as an argument and deallocate that memory when the scope closes. You can see in the declaration of the function that whatever type we return from the block (R) will be the same value returned from the call tomemScoped(R):inline fun <R> memScoped(block: MemScope.() -> R): RThe
MemScopedobject allows calling theallocmethod to allocate memory for use within this block, and we need to allocate memory for aBooleanVar. Note thatallocreturns typeTwhich conforms toCVariable.Populating the value of
isDirectoryWhen calling the
fileExists()method in K/N, if we check the K/N header declaration we can see what it expects to receive for arguments (formatted for readability). Note theisDirectoryparameter is aCPointerpointing to aBooleanVar. This is how we knew to allocate memory for aBooleanVarand not some other kind of type:Therefore, with the memory that we allocated, which is of type
CVariable, we can pass the pointer to that memory (passing by reference) for thefileExistsAtPathmethod to change the value of it:Lastly, getting the value populated into the
BooleanVarCVariableis accessed from the.valueproperty.Together, the code might look something like:
iOS URL
As for converting the entire method, this is getting a bit into API generalization. You'll need to convert the
URLargument to a platform-independent type, such as a simple path (string). This appears to be unrelated to your original question, so although I'll leave this detail out, you might find this blog post helpful.If you want to go deeper
Kotlin/Native has some documentation on its interoperability with C, however admittedly, I find this documentation a bit difficult to understand with the provided example.