Tag: kotlin

“kotlin-noarg”插件在Realm中不起作用

“kotlin-allopen”插件工作,但“kotlin-noarg”插件不工作。 我能怎么做? 下面是代码。 的build.gradle buildscript { ext.kotlin_version = '1.1.3-2' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-beta2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version" classpath "io.realm:realm-gradle-plugin:3.5.0" } } apply plugin: "kotlin-allopen" apply plugin: "kotlin-noarg" allOpen { annotation("sample.AllOpen") } noArg { annotation("sample.NoArg") invokeInitializers = true } 应用程序/的build.gradle apply plugin: 'realm-android' NoArg.kt @Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.SOURCE) annotation class NoArg […]

Kotlin + MVP – 意外覆盖

我一起使用Kotlin和MVP,偶然发现有点烦恼。 我得到一个“意外覆盖”的错误(你可以从下面看出来)。 除了在MainView界面中更改成员变量名称或getX()之外,是否还有解决此问题的方法。 从我所研究的,没有办法阻止kotlin为'x'生成吸气剂。 class MainActivity : Activity(), MainView { val x: String // Accidental override override fun getX(): String { return x } } interface MainView { fun getX(): String }

Kotlin为不同的增量

Kotlin有跟随 for (i in 0..10) {} 它类似于Java for (int i = 0; i < 10; i++) {} 但如何改变kotlin的增量,以获得类似于java的东西: for (int i = 0; i < 10; i = i + 2) {}

了解kotlin执行者

我理解执行者的概念,但是我在理解kotlin的执行者方面有些麻烦。 也许这是语法没有帮助。 让我们看看下面的例子: private class AlwaysCallback(private val executor: (() -> Unit) -> Unit, private val cb: Progress.() -> Unit) : Callback { override fun execute(progress: Progress) { executor { progress.cb() } } } 如果我理解正确,执行者( () -> Unit ) -> Unit是一个关闭的容器。 要执行的代码块。 我不确定这是真的,还是只是一个匿名功能的船只。 另一件事是,有人可以解释语法: ( () -> Unit ) -> Unit ? 我已经阅读了kotlin文档,阅读kotlin源代码,并试图谷歌,但我真的很难理解这一点。 谢谢

Kotlin:更新不可变列表元素

Kotlin初学者在这里。 我如何获取一个列表,而不用改变它,创建一个更新元素在特定索引的第二个(不可变)列表? 我想到了两种方式,这两种方式似乎都可能导致性能命中,改变底层对象,或两者兼而有之。 data class Player(val name: String, val score: Int = 0) val players: List<Player> = … // Do I do this? val updatedPlayers1 = players.mapIndexed { i, player -> if (i == 2) player.copy(score = 100) else player } // Or this? val updatedPlayer = players[2].copy(score = 100) val mutable = players.toMutableList() mutable.set(2, […]

Kotlin通用属性

在没有声明类级别泛型类型的情况下 ,kotlin有没有办法创建泛型属性? 看起来像这样的东西: interface Generic { val t: T //I need this type only once, thats why I dont wanna pass in the class level fun <E> gMethod(e: E) { //This works, so I'm wondering if there's something similiar to properties } }

Android Studio 3 canary&Kotlin

有没有人得到消息“Kotlin未配置” 我确定我错过了一些东西,但是大部分我下载了它,并且只是导入了我现有的2.x设置。 我尝试从java类复制/粘贴到一个新的Kotlin文件,这就是我在哪里。 这是我目前的顶级build.gradle buildscript { repositories { jcenter { url = "http://jcenter.bintray.com/" } maven { url = "https://maven.google.com" } mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha1' } } allprojects { repositories { jcenter { url = "http://jcenter.bintray.com/" } maven { url = "https://maven.google.com" } mavenCentral() } } 然后在我的模块构建文件的顶部我有这两行 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' 目前的错误是“插件与id'kotlin-android'未找到”

在Kotlin中使用Javascript库

我最后一次使用Kotlin的时候是2015年12月,当时我用它来解决一些欧拉项目问题。 这一次我想尝试与Javascript的互操作性。 现在我的问题是,我们如何在Kotlin中导入/使用现有的Javascript库? 我见过一些使用native关键字的人,我只是想简单地解释一下。

Kotlin中的保留关键字是什么?

我走过https://kotlinlang.org/docs/reference,但我找不到保留关键字,在Kotlin中使用。 Kotlin有多少个关键字? 只要我们知道Java有像这样的自己的关键字列表:

将GsonBuilder转换成Kotlin

有谁知道如何将此代码转换为kotlin GsonBuilder builder = new GsonBuilder(); builder.setLenient(); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { @Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.getAsJsonPrimitive().isNumber()) { return new Date(json.getAsJsonPrimitive().getAsLong() * 1000); } else { return null; } } }); return builder.create(); 然后,我试了一下 val builder = GsonBuilder() builder.setLenient() builder.registerTypeAdapter(Date::class.java,………) return builder.create() …..我不知道如何转换代码