如何使用kotlin Multiplatform和Swift的默认接口实现
我正在使用KMP在Android和iOS上制作一个跨平台应用程序。我的问题是,当我使用默认实现在Kotlin common main中创建接口时,我不能在iOS中使用该实现,我需要在Xcode中重写代码。在我的方法中没有特定于平台的内容。它们只是与数字一起工作的函数(因此,只是一些for或if,带有返回的数字参数),所以对我来说,只需编写一次方法就可以了。有办法做到这一点吗 这是Kotlin中带有方法和默认实现的接口:如何使用kotlin Multiplatform和Swift的默认接口实现,swift,interface,swift-protocols,kotlin-multiplatform,default-method,Swift,Interface,Swift Protocols,Kotlin Multiplatform,Default Method,我正在使用KMP在Android和iOS上制作一个跨平台应用程序。我的问题是,当我使用默认实现在Kotlin common main中创建接口时,我不能在iOS中使用该实现,我需要在Xcode中重写代码。在我的方法中没有特定于平台的内容。它们只是与数字一起工作的函数(因此,只是一些for或if,带有返回的数字参数),所以对我来说,只需编写一次方法就可以了。有办法做到这一点吗 这是Kotlin中带有方法和默认实现的接口: interface StandardValues { fun nextVal
interface StandardValues {
fun nextValue(valueToCompare: String, currentStandardSpinner: String, currentUnitSpinner: Int): Pair<String, Int> {
var currentStandardArray = StandardValue.E12.valuesStandard
var indexSpinner = currentUnitSpinner
var valueConverted = 0.0f
try{
valueConverted = valueToCompare.toFloat()
} catch (e: NumberFormatException){
e.printStackTrace()
println(e)
}
if(valueToCompare.isNotEmpty()){
var previousValue: Float = 0.0f
if(valueConverted <= 1 && currentUnitSpinner == 0){
return Pair("0",0)
}
if(valueConverted < 1) {
for ((index, value) in currentStandardArray.withIndex()){
if ((valueConverted * 1000) > value){
previousValue = currentStandardArray[index]
}
}
if(indexSpinner != 0){
indexSpinner--
}
return Pair(previousValue.toString(), indexSpinner)
}
if(valueConverted <= currentStandardArray.first()){
if(indexSpinner == 0){
return Pair(currentStandardArray.first().toString(), 0)
}
previousValue = currentStandardArray.last()
indexSpinner--
return Pair(previousValue.toString(), indexSpinner)
}
for ((index, value) in currentStandardArray.withIndex()){
if (valueConverted > value){
previousValue = currentStandardArray[index]
}
}
return Pair(previousValue.toString(), indexSpinner)
}
return Pair(currentStandardArray.first().toString(), indexSpinner)
}
在Xcode中:
class MeasureConverterViewController: UIViewController, StandardValues {
func nextValue(valueToCompare: String, currentStandardSpinner: String, currentUnitSpinner: Int) -> (String, Int) {
//I would like to avoid to write the same logic code
}
textView.text = nextValue(nextValue("3", "pF", 0).0
...
}
否则,我认为我将在Kotlin中实现接口,并在Swift中创建一个带有扩展的协议
谢谢。为了解决这个问题,我在Android Studio中实现了接口,在同一个文件中,我创建了一个实现接口的类,因此在Xcode中,我可以实例化该类的对象以使用默认方法 安卓工作室:
interface StandardValues {
... //default methods
}
class StandardValuesImplementation: StandardValues{}
Xcode:
class MyViewController: UIViewController{
...
override func viewDidLoad() {
super.viewDidLoad()
...
let myClassImpl = StandardValuesImplementation()
let textView = MyClassImpl.myDefaultMethod
...
}
谢谢,我已经添加了代码。我没有写它,因为我的问题是:一般来说,如何在Xcode中使用默认方法实现(在Android Studio中与Kotlin一起编写):)我对错误进行了分析。Kotlin接口映射到ObjC中的协议。这意味着它不能有默认的实现。您需要将该类从接口更改为类,并在使用该类时使用合成。感谢您的回复。为了解决这个问题,我在androidstudio中实现了接口,在同一个文件中,我创建了一个类来实现我的接口,因此在Xcode中,我可以实例化该类的一个对象以使用默认方法。这可能是一个正确的解决方案吗?这很有帮助-谢谢!在我的例子中,我还想用一个方法的一些特定于Swift的实现细节来扩展Xcode中的类,为此,我需要将共享类(
StandardValuesImplementation
,在您的示例中)标记为open class
。
class MyViewController: UIViewController{
...
override func viewDidLoad() {
super.viewDidLoad()
...
let myClassImpl = StandardValuesImplementation()
let textView = MyClassImpl.myDefaultMethod
...
}