Tag: 科特林

Kotlin投影冗余

在阅读关于Kotlin的genericstypes变化和投影时,我想出了一个完全陌生的概念。 有人可以解释一下作者想要解释的想法是什么? 请参阅Kotlin在行动中的引用,MEAP : 没有意义得到一个已经out差异的types参数的out ,例如List 。 这意味着与List相同,因为List被声明为class List 。 Kotlin编译器会警告这样的预测是多余的。 这里有两个具体的问题: 为什么你需要添加投影已经超出投影types列表? 即使你这样做,你如何得到相同的列表?

kotlin中的“::”是什么意思?

我是Kotlin的新手 我用这个代码打开另一个活动: startActivity(Intent(this,IntroAndLang::class.java)) 目前的活动和目标活动写在Kotlin 我不明白为什么没有单一的:而不是::在IntroAndLang::class.java

Android / Kotlin:未解决的参考:木材

我正在尝试为Android编写一个kotlin库,不能包含木材。 我总是得到以下错误: Error:error: unresolved reference: timber 我有这个在我的build.gradle: apply plugin: ‘java-library’ apply plugin: ‘kotlin’ dependencies { implementation fileTree(dir: ‘libs’, include: [‘*.jar’]) compile “org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version” } sourceCompatibility = “1.8” targetCompatibility = “1.8” buildscript { ext.kotlin_version = ‘1.1.2-4’ repositories { maven {url “https://maven.google.com”} mavenCentral() } dependencies { classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” } } repositories { mavenCentral() } dependencies { compile “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” […]

原生iOS项目中的Kotlin文件与Kotlin / Native

我想包含一个Kotlin文件,它只在现有的iOS项目中执行数据处理和网络操作,同时保留原生的iOS UI代码。 虽然我认为这可以通过Kotlin / Native实现,但我发现使用Kotlin / Native的iOS示例( 1,2 )似乎也接管了iOS UI代码。 是否包含一个Kotlin文件,用于在iOS中使用Kotlin / Native进行数据传输,而不需要触摸UI代码,如果是这样,那么步骤如何?

如何从命令行运行Kotlin类?

我明白这个问题之前已经被问过了,但没有任何信息帮助过我。 这是我的情况:我不能运行一个编译的Kotlin类。 当我尝试运行它像我会正常的java类,我得到以下内容: C:\Users\User\Desktop>java _DefaultPackage Exception in thread “main” java.lang.NoClassDefFoundError: jet/runtime/Intrinsics at _DefaultPackage.main(Finder.kt) Caused by: java.lang.ClassNotFoundException: jet.runtime.Intrinsics at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) … 1 more 这让我相信Kotlin的运行时间只是缺少,给出了输出。 所以我尝试了以下几点: C:\Users\User\Desktop>java -cp kotlin-runtime.jar _DefaultPackage Error: Could not find or load main class _DefaultPackage 这使我想,也许我需要将类文件添加到我声明的类路径,所以: C:\Users\User\Desktop>java -cp kotlin-runtime.jar’;_DefaultPackage.class _DefaultPackage […]

科特林位移

我想转换代码如果这个答案Kotlin: https : //stackoverflow.com/a/5402769/2735398 我把这个贴到了Intellij: private int decodeInt() { return ((bytes[pos++] & 0xFF) << 24) | ((bytes[pos++] & 0xFF) << 16) | ((bytes[pos++] & 0xFF) << 8) | (bytes[pos++] & 0xFF); } Intellij问我是否要把它转换成Kotlin,当我这样做的时候是输出: private fun decodeInt(): Int { return (bytes[pos++] and 0xFF shl 24 or (bytes[pos++] and 0xFF shl 16) or (bytes[pos++] and 0xFF shl […]

Kotlin递归

fun fact(x: Int): Int{ tailrec fun factTail(y: Int, z: Int): Int{ if (y == 0) return z else return factTail(y – 1, y * z) } return factTail(x, 1) } 有人请向我解释一下上面的递归函数在kotlin中是如何工作的?

Kotlin抛出NullPointerexception

我开始和Java一起使用Kotlin。 这在java下面的代码。 抛出空指针exception initFoodQuantitySpinner(); CartDb cartDb = new CartDb(getActivity()); try { P.addToCart(getActivity(), food.getId(), food.getPrice(), foodQuantity); } catch (Exception e) { e.printStackTrace(); } FoodCartModel foodCartModel = new FoodCartModel(new Food(food.getId(), food.getName(), 0, “”, “”, food.getPrice(), 0, 0, null, “”, “”, null)); cartDb.insertFood(foodCartModel); if (getActivity() != null) getActivity().invalidateOptionsMenu(); 下面的代码在Kotlin中抛出空指针exception。 spinFoodQuantity.visibility = View.VISIBLE val cartDb = CartDb(context) val id […]

val … get(){…}在Kotlin

从Kotlin Koan问题( https://github.com/Kotlin/kotlin-koans/blob/master/src/ii_collections/n16FlatMap.kt ),我有这个Koan代码。 我如何阅读这个? 它看起来像一个val的variables,但是它是一个带有()和{}的函数。 val Customer.orderedProducts: Set get() { // Return all products this customer has ordered todoCollectionTask() }

Kotlintypes推断faild

当我在项目中使用Kotlin和Java时在Java BaseActivity.class中: public abstract Class bindViewModel(); 当我在Kotlin中扩展BaseActivity时: override fun <T : BaseViewModel<*, out IBaseView>?> bindViewModel(): Class { return ArchViewModel::class.java } Kotlin提醒我返回是types推断expression式 Type inference failed. Expected type mismatch: required:Class found:Class 如何解决这个问题? PS的ArchViewModel.class扩展BaseViewModel