简体   繁体   English

如何在 KMM(kotlin 本机)共享模块中读取 Swift 框架库中生成的对象数组

[英]How to read an array of objectes generated in a Swift framework library, in a KMM (kotlin native) shared module

I'm working on a KMM app.我正在开发 KMM 应用程序。 The shared module has a helper class, that relies on different native libraries for the android part and for the iOS part.共享模块有一个帮助器 class,它依赖于 android 部件和 iOS 部件的不同本机库。 This is implemented with the already know "expected/actual" pattern.这是通过已知的“预期/实际”模式实现的。

As said, the iOS actual class makes use of an iOS framework, that performs some calculations, and returns an array of objects.如前所述,iOS 实际 class 使用了 iOS 框架,该框架执行一些计算,并返回一个对象数组。 The ios framework that creates the list of objects works correctly (it was tested with unit tests).创建对象列表的 ios 框架工作正常(已通过单元测试进行测试)。 A simplified example follows below.下面是一个简化的示例。

This is the class of the objects that are inside of the array:这是数组内部对象的 class:

public class ChildNodeIos:  NSObject{

public  let content:String
public let isTextNode:Bool

public init(content:String,isTextNode:Bool=false){
    self.content=content
    self.isTextNode=isTextNode
}

} }

The helper class on the iOS side that returns the list of objects would be something like that:返回对象列表的 iOS 端的助手 class 将类似于:

@objc public class IOSCoolHelper: NSObject {
    @objc public func getChildNodes(message: String) -> [ChildNodeIos]  {
       //build the array of child nodes here and return them
     }
}

In the kotlin shared module, inside the iOS expected class, the function is called like the following:在 kotlin 共享模块中,在 iOS 内部预期 class,ZC1C425268E68385D14AB5074C17ZA 如下所示:

@Serializable
data class ChildNodeKN(val content :String,val isTextNode :Boolean=false)

import com.mydomain.iosframeworks.IosCoolHelper
actual class CoolHelper actual constructor(private val someStuff: String)  : ICoolHelper {

       actual override fun getChildNodes(message: String): List<ChildNodeKN> {
            val iosHelper= IOSCoolHelper()
            val swiftarray:List<ChildNodeIos>  = iosHelper.getChildNodes(message)
    
            //I was expecting to do something like that but it does not work (the content of "array is always empty"):
    
            val kotlinList:List<ChildNodeKN> = swiftarray as List<ChildNodeIos>
    
         return kotlinList
    }
        }
    
    }

Or maybe if the list of swift objects can not be direct be casted to the equivalent kotlin object list, I was expecting to be able to iterate over the swift list and convert it to the kotlin list, something like that: Or maybe if the list of swift objects can not be direct be casted to the equivalent kotlin object list, I was expecting to be able to iterate over the swift list and convert it to the kotlin list, something like that:

    val kotlinList=mutableListOf<ChildNodeKN>()
swiftArray.foreach{
kotlinList.add(ChildNodeKN(it.content,it.isTextNode))
}

But again, the content of the swift Array is empty.但同样,swift 数组的内容是空的。 Doing a lot of tests (I can no reproduce them now), I managed to access something inside the array, but it was not an object of type ChildNodeIos, nor something I could read on the kotlin side.做了很多测试(我现在无法重现它们),我设法访问了数组内部的一些东西,但它不是 ChildNodeIos 类型的 object,也不是我在 kotlin 端读不到的东西。

Well, the question is, how to receive on the kotlin side, a list with more or less complex objects inside, that was generated on the iOS side?好吧,问题是,如何在 kotlin 端接收在 iOS 端生成的内部或多或少复杂对象的列表?

I have to say, that this swift helper class has many other functions that return primitive values (strings, booleans, or int), and that is working very well.我不得不说,这个 swift 助手 class 具有许多其他返回原始值(字符串、布尔值或 int)的函数,并且运行良好。

I suppose a workaround would be instead an array with objects, to return an array with primitive types and two dimensions from the Swift side, but I would like to work with an array of objects if it is possible.我想一个解决方法将是一个包含对象的数组,从 Swift 端返回一个具有基本类型和二维的数组,但如果可能的话,我想使用一个对象数组。

Thank you for your help谢谢您的帮助

I managed to find the solution by myself.我设法自己找到了解决方案。 The problem was the declaration of the Swift class of the object contained in the list.问题是列表中包含的 object 的 Swift class 的声明。 I forgot the @objc declaration for the properties of the class, because if that I was not able to read the objects inside the returned array.我忘记了 class 属性的 @objc 声明,因为如果那样我就无法读取返回数组中的对象。

public class ChildNodeIos: NSObject{公共 class ChildNodeIos:NSObject{

@objc  public  let content:String
@objc  public let isTextNode:Bool

public init(content:String,isTextNode:Bool=false){
    self.content=content
    self.isTextNode=isTextNode
}

} }

And then, on the Kotlin side, I did not achieve to cast it directly to a list, but with a foreach loop it is very easy to write the iOS objects in Kotlin objects:然后,在 Kotlin 方面,我没有实现将其直接转换为列表,但使用 foreach 循环很容易在 Kotlin 对象中编写 iOS 对象:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM