Kotlin JS JSON反序列化

我在javascript平台上使用kotlin。 此代码在第六行上失败,但“Uncaught TypeError:aciterator不是函数”异常。 class A(val b: String, val c: List<String>) fun main(args: Array<String>) { val a = JSON.parse<A>("""{"b": "b_value", "c": ["c_value_1", "c_value_2"]}""") println(ab) for (c in ac) println(c) } 在JavaScript的debuger我可以看到,该对象“一”是反序列化。 但我认为,它不是一个有效的类型A的kotlin对象。有什么办法,如何从对象“a”或从原始的json字符串创建类型A的有效kotlin对象实例?

Kotlin通用边界

我正试图将我的MVP解决方案从JAVA移植到Kotlin,而我在泛型方面遇到了一个很大的问题。 这是我的JAVA类的样子: public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView public abstract class BasePresenter<V extends BaseView> public interface BaseView 并移植到Kotlin: abstract class BaseActivity<P : BasePresenter<BaseView>> : AppCompatActivity(), BaseView abstract class BasePresenter<V : BaseView> interface BaseView 而当我试图使用 class MainActivity() : MainView, BaseActivity<MainPresenter>() 我得到“类型参数不在其范围内,预期: BasePresenter , 找到MainPresenter ”

过时的Kotlin运行时间

当我想更新kotlin运行时时,我收到了这条消息: “kotlin-stdlib-1.1.2-4”库中的Kotlin运行时版本是1.1.2-4,而插件版本是1.1.3-release-Studio 3.0-2。 运行时库应该更新以避免兼容性问题。 文件build.gradle中存在一些代码段 ext.kotlin_version ='1.1.2-4' classpath'com.android.tools.build:gradle:3.0.0-alpha8' 任何人都可以帮我解决这个问题? 提前致谢..

不能从java模块调用kotlin模块

我想在java中有一个android应用程序,但是在kotlin中有一个库模块。 但是,当我尝试在手机中运行应用程序时,出现错误,说找不到我的Kotlin类。 这是我的科林类: package com.example.mylibrary import android.util.Log class A { fun helloWorld(){ Log.d("Kotlin", "Hello World!") } } 和我的kotlin模块的gradle文件: apply plugin: 'com.android.library' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { minSdkVersion 11 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main.java.srcDirs += 'src/main/kotlin' } […]

Kotlin中的Java注释参数

我正在试验Kotlin,并且有一个下面的Java注释 @Target( { TYPE }) @Retention(RUNTIME) public @interface View { String[] url() default ""; Class<? extends Component> parent() default Component.class; } 在Java代码中,它被用于以下的方式 @View(url="/", parent=RootView.class) public class FrontView extends Component { } Kotlin是如何表达的? 我努力了 [View(url=Array<String>("/"), parent=Class<RootView>)] class FrontView : Component() { } 但它不编译。 我只有类型不匹配的错误。 Type mismatch. Required: jet.Array<jet.String?>? Found: jet.Array<T> 和 Type mismatch Required: java.lang.Class<out net.contextfw.web.application.component.Component?>? […]

Kotlin注释IntDef

我有这个代码示例: class MeasureTextView: TextView { constructor(context: Context?) : super(context) constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) companion object{ val UNIT_NONE = -1 val UNIT_KG = 1 val UNIT_LB = 0 } fun […]

Kotlin setOnclickListener

回到java中,我曾经写过只return一个无效的方法…但kotlin似乎不允许只是返回,而是使用return@methodname ? 有人可以解释这是什么,它是如何增加价值? bAddLine.setOnClickListener { val selectedSeries = getSelectedSeries() if (selectedSeries.isEmpty()) { Toast.makeText(this, getString(R.string.toast_channel_mandatory), Toast.LENGTH_LONG).show() return@setOnClickListener } }

Kotlin特质和改造

我正在努力在Kotlin实施这个例子。 我在运行时得到这个错误“引起:java.lang.IllegalArgumentException:接口定义不能扩展其他接口”。 当没有任何方法实现时,有什么方法可以将Kotlin特性当作java接口处理,还是必须使用java接口? 代码如下: public trait RestAPI { [GET("/weather")] fun getList([Query("q")] place: String, [Query("units")] units: String) : Observable<WeatherData> } 而导致错误的调用: val service = restAdapter?.create(javaClass<RestAPI>())

Kotlin和WebSockets

我正在制作一个小型的服务器端应用程序,它将通过WebSockets与浏览器进行通信,并且正在考虑将其写入Kotlin。 是否有一个WebSockets API可以和Kotlin一起使用,或者任何Java WebSockets API都适合这个账单?

索引在Kotlin数组中

我如何从一个Kotlin数组中获得一个值的索引? 我现在最好的解决方案是使用: val max = nums.max() val maxIdx = nums.indices.find({ (i) -> nums[i] == max }) ?: -1 有没有更好的办法?