简体   繁体   English

无法使用 Kotlin 分机 Function 在 iOS 中共同编写为 Swift 分机

[英]Not able to use Kotlin Extension Function written in common in iOS as Swift Extension

I have a Kotlin Multiplatform project setup with Android & cocoapods for iOS.我有一个 Kotlin 多平台项目设置,其中 Android 和 cocoapods 为 iOS。

I have a file Extensions.kt in commonMain/src with the following function:我在commonMain/src中有一个文件Extensions.kt ,其中包含以下 function:

fun String.isValidEmail(): Boolean {
    return validate(ValidatorRegex.EMAIL)
}

I am able to access this function in Android as a String extension:我可以在 Android 中访问这个 function 作为String扩展:

"abcd@gmail.com".isValidEmail()

But in iOS using Swift, I need to call it as a static method of another class:但是在iOS中使用Swift,我需要调用它作为另一个class的static方法:

ExtensionsKt.isValidEmail("abcd@gmail.com")

It should convert that commonMain/src method to a Swift extension of String instead of a class with a static method.它应该将该commonMain/src方法转换为String的 Swift 扩展,而不是带有 static 方法的 class。

Am I missing any configuration?我是否缺少任何配置?

You're doing everything right here.你在这里做一切。 Unfortunately, this option is not available for now.不幸的是,此选项目前不可用。 Extensions conversion may be performed correctly for some classes, but Swift's String is not the one.某些类的扩展转换可能会正确执行,但 Swift 的String不是这样。 This is a known inconvenience, and K/N team is working hard to make it better.这是一个众所周知的不便,K/N 团队正在努力改进它。

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

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