我正在iOS上尝试使用Kotlin Native,我想尝试的一件事是使用Swot实现Kotlin定义的接口.但是,当我尝试将Swift对象传递回Kotlin代码时,我最终崩溃了.
我正在使用kotlin gradle插件版本1.2.30和kotlin本机版本0.6.1
最小的例子如下.Kotlin代码正在编译为名为KotlinCommon的框架,然后将其包含在xcode项目中.
DemoClass.kt
class DemoClass {
fun process(dependency: Dependency) {
dependency.foo()
}
}
interface Dependency {
fun foo()
}
Run Code Online (Sandbox Code Playgroud)
SwiftDependency.swift
import KotlinCommon
class SwiftDependency : KotlinCommonDependency {
func foo() {
NSLog("hello")
}
}
Run Code Online (Sandbox Code Playgroud)
然后,从我的iOS UI尝试运行
let module = KotlinCommonDemoClass()
let dependency = SwiftDependency()
module.process(dependency: dependency)
Run Code Online (Sandbox Code Playgroud)
第三行导致崩溃,并出现以下错误:
*** NSForwarding: warning: object 0x101858d20 of class 'KotlinNativeDemo.SwiftDependency' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[KotlinNativeDemo.SwiftDependency toKotlin:]
Run Code Online (Sandbox Code Playgroud)
Kotlin Native不支持此用例吗?或者可能有一些我配置错误的东西?
所以我尝试使用intellij创建kotlin/native应用程序(我在项目创建中选择了模板kotlin-> kotlin/native).它创建了示例gradle hello world项目.下载后所有依赖项编译成exe文件并正常运行.但现在我需要包含som库,我无法弄清楚如何.首先,我只想包含任何jar库(例如jackson-core).这就是build.gradle文件的样子:
plugins {
id 'kotlin-multiplatform' version '1.3.0'
}
repositories {
mavenCentral()
}
kotlin {
targets {
// For ARM, preset should be changed to presets.iosArm32 or presets.iosArm64
// For Linux, preset should be changed to e.g. presets.linuxX64
// For MacOS, preset should be changed to e.g. presets.macosX64
fromPreset(presets.mingwX64, 'mingw')
configure([mingw]) {
// Comment to generate Kotlin/Native library (KLIB) instead of executable file:
compilations.main.outputKinds('EXECUTABLE')
// Change to specify fully qualified name of your application's entry point:
compilations.main.entryPoint = …Run Code Online (Sandbox Code Playgroud) Java中的Closeable接口提供了一种方便的抽象,可以方便地管理可关闭的资源。在多平台 kotlin 的背景下,是否有一种模式、实践或功能可以帮助打破共享/多平台 Closeable 接口和实际的 Java Closeable 接口之间的差距,因为它们必然是两种不同的类型?
无法关闭类型差异和/或具有标准库可关闭的影响是 Closeable 接口的激增,即使它们本质上是相同的东西,也无法跨库组合。
进行编码挑战时的常见模式是读取多行输入。假设您事先不知道多少行,那么您要读取直到EOF(readLine返回null)。
另外,作为序言,我不想依赖java.utils。*,因为我使用KotlinNative进行编码,所以没有Scanner。
我想做些类似的事情
val lines = arrayListOf<String>()
for (var line = readLine(); line != null; line = readLine()) {
lines.add(line)
}
Run Code Online (Sandbox Code Playgroud)
但这显然不是有效的Kotlin。我能想到的最干净的是:
while (true) {
val line = readLine()
if (line == null) break
lines.add(line)
}
Run Code Online (Sandbox Code Playgroud)
这行得通,但似乎不是很惯用。有没有更好的方法可以将所有行读入数组,而无需使用while / break循环?
所以我知道Kotlin Native显然是Native,而Kotlin JVM不是,但是Kotlin JVM和Kotlin Native之间的代码是:1.不同的编译器和不同的代码2.不同的编译器和类似的代码3.不同的编译器和相同的代码4.没有以上(请说明)
我想使用像 getTimeMillis() 这样的系统函数,它应该是 kotlin.system 的一部分:https ://kotlinlang.org/api/latest/jvm/stdlib/kotlin.system/index.html
但是编译器说不能导入这样的模块。gradle 配置是这样的(kotlin 多平台项目):
commonMain.dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:1.3.10"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.10.0"
implementation "io.ktor:ktor-client:1.0.0"
implementation "io.ktor:ktor-client-logging:1.1.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.1.0"
}
Run Code Online (Sandbox Code Playgroud)
我也找不到任何使用示例或此模块。
我想尝试构建依赖于某些库的 Kotlin/Native 项目。正如文档所解释的,我需要创建 def 文件(我已经创建了)并运行cinterop工具。
但是,我无法在我的 Mac 上找到这个工具,并且很好奇如何安装它。
你能给我一些建议吗?
我目前正在开发 Kotlin 多平台,并尝试与 GoogleWebRTC pod 进行交互。
该 Pod 存在,我能够将其导入到单独的项目中(没有 Kotlin 多平台)。我可以看到它.framework是在我的构建目录中创建的,但是当执行 gradle 同步或从 Xcode 构建时,我收到以下错误:
Exception in thread "main" java.lang.Error: /var/folders/hv/9cx28nxx4gz9hj_m86bp5rx40000gn/T/tmp362966650322311128.m:1:9: fatal error: module 'GoogleWebRTC' not found
at org.jetbrains.kotlin.native.interop.indexer.UtilsKt.ensureNoCompileErrors(Utils.kt:152)
at org.jetbrains.kotlin.native.interop.indexer.ModuleSupportKt.getModulesASTFiles(ModuleSupport.kt:67)
at org.jetbrains.kotlin.native.interop.indexer.ModuleSupportKt.getModulesInfo(ModuleSupport.kt:13)
at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.buildNativeLibrary(main.kt:499)
at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.processCLib(main.kt:264)
at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.interop(main.kt:72)
at org.jetbrains.kotlin.cli.utilities.InteropCompilerKt.invokeInterop(InteropCompiler.kt:45)
at org.jetbrains.kotlin.cli.utilities.MainKt.mainImpl(main.kt:19)
at org.jetbrains.kotlin.cli.utilities.MainKt.main(main.kt:37)
Execution failed for task ':shared:cinteropGoogleWebRTCIosArm64'.
> Process 'command '/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
Run Code Online (Sandbox Code Playgroud)
AFNetworking 似乎工作正常,但添加 WebRTC pod 似乎破坏了该项目。
我是否错过了 Kotlin 多平台的限制或其他什么?
这是我的build.gradle.kts:
cocoapods {
// Configure fields required …Run Code Online (Sandbox Code Playgroud) 我简化了错误,我只有这个类:
class TestClass{
private var string = "Hello"
fun testError() {
string= "It Works"
GlobalScope.launch(Dispatchers.Default) {
string = "Doesn't work"
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在主线程(在 IOS 上)上启动 TestClass().testError(),它会抛出一个 InvalidMutabilityException(在 line --> string = "Doesn't work")。所以我认为在创建变量的线程之外的线程上更改变量可能不是一个好主意。所以我改成这样:
class TestClass{
private var string = "Hello"
fun testError() {
string= "It Works"
GlobalScope.launch(Dispatchers.Default) {
withContext(Dispatchers.Main) { string = "Doesn't work" }
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它仍然抛出一个错误:
kotlin.native.concurrent.InvalidMutabilityException:冻结 com.example.project.TestClass@fe10a8 的突变尝试
顺便一提。上面的两个代码都适用于 Android 端
我正在关注此处的Kotlin/Native Concurrency 实践。我无法理解最后一个示例,为什么“上面修改后的 saveToDb 函数现在处理后台调用,并且仅捕获函数参数。这不会冻结父类”
class CountingModelSafer{
var count = 0
fun increment(){
count++
saveToDb(count)
}
private fun saveToDb(arg:Int) = background {
println("Doing db stuff with $arg, in main $isMainThread")
}
}
Run Code Online (Sandbox Code Playgroud)
[请注意,在这些示例中,在背景内部,传递的 lambda 被冻结]
下面的代码片段会导致整个 CountingModel 被冻结,但上面的代码片段不会被冻结。有人可以帮我理解为什么会这样吗?
class CountingModel{
var count = 0
fun increment(){
count++
background {
saveToDb(count)
}
}
private fun saveToDb(arg:Int){
//Do some db stuff
println("Saving $arg to db")
}
}
Run Code Online (Sandbox Code Playgroud) kotlin-native kotlin-multiplatform kotlin-multiplatform-mobile
kotlin-native ×10
kotlin ×8
android ×1
cinterop ×1
ios ×1
java ×1
jvm ×1
kotlin-multiplatform-mobile ×1
swift ×1