将可空types转换为不可空types?

我有一堆有可空属性的bean,如下所示: package myapp.mybeans; data class Foo(val name : String?); 我在全球空间里有一个方法就是这样: package myapp.global; public fun makeNewBar(name : String) : Bar { … } 而在其他地方,我需要从Foo内部的东西上制作一个Bar 。 所以,我这样做: package myapp.someplaceElse; public fun getFoo() : Foo? { } … val foo : Foo? = getFoo(); if (foo == null) { … return; } // I know foo isn’t null […]

Kotlin’直到’创建IntRange垃圾的乐趣

我正在使用until缀乐趣为我的循环下面 for (x in 0 until bodies.size) bodies[x]() 当用YourKit分析我的代码时,我注意到我有大量的IntRange对象(约2k /秒)。 当我切换循环使用int…int rangeTo直接它不会创建任何垃圾。 for (x in 0..bodies.size-1) bodies[x]() 有人可以解释这两者之间的区别吗? 从我可以告诉Int.until简单地返回this .. to public infix fun Int.until(to: Int): IntRange { val to_ = (to.toLong() – 1).toInt() if (to_ > to) throw IllegalArgumentException(“The to argument value ‘$to’ was too small.”) return this .. to_ }

为什么添加谷歌的火力点引起Dex错误?

添加 实现“com.google.firebase:firebase-core:11.8.0” 到我的模块gradle文件给我错误提示我有超过64K方法在我的项目。 我的猜测是,我以某种方式实现了整个谷歌播放服务,虽然增加 实现’com.google.android.gms:play-services-maps:11.8.0′ 要么 实现’com.google.android.gms:play-services-location:11.8.0′ 给我没有错误。 格拉德尔: apply plugin: ‘com.android.application’ apply plugin: ‘kotlin-android’ apply plugin: ‘kotlin-android-extensions’ android { compileSdkVersion 26 buildToolsVersion “26.0.2” defaultConfig { applicationId “se.workshop.collect” minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName “1.0” testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner” vectorDrawables.useSupportLibrary = true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’ } } } dependencies […]

从Long到ByteArray的两次转换有什么区别?

import java.lang.Long as JLong import java.lang.Byte as JByte import java.nio.ByteBuffer fun Long.toByteArray1() = ByteBuffer.allocate(JLong.SIZE / JByte.SIZE) .putLong(this) .array() fun Long.toByteArray2() = this.toString() .toByteArray(Charsets.UTF_8) fun main(args: Array) { val a1: ByteArray = 10L.toByteArray1() val a2: ByteArray = 10L.toByteArray2() println(“a1 = ${a1.toString()}”) println(“a1 = ${ByteBuffer.wrap(a1).getLong()}”) println(“a2 = ${a2.toString()}”) println(“a2 = ${String(a2, Charsets.UTF_8)}”) } toByteArray1()和toByteArray2()有什么区别? 如果我发送字节到输出流接收器将得到?

在Kotlin的`forEach`中`break`和`continue`

Kotlin有非常好的迭代函数,比如forEach或者repeat ,但是我不能让break和continue它们(local和non-local): repeat(5) { break } (1..5).forEach { continue@forEach } 目标是尽可能接近地使用函数式语法来模仿通常的循环。 这在Kotlin的一些老版本中是绝对有可能的,但我很难重现这个语法。 问题可能是标签(M12)的一个错误,但我认为第一个例子应该工作。 在我看来,我已经读了一些关于特殊技巧/注释的地方,但我找不到任何关于这个主题的参考。 可能看起来像下面这样: public inline fun repeat(times: Int, @loop body: (Int) -> Unit) { for (index in 0..times – 1) { body(index) } }

是否有一个相当于Swift init Kotlin(重复:计数:)

我只是想知道是否有一个相当于Swift初始化程序初始化Kotlin init(repeating:count:) 例如,创建具有空值的数组的数组非常有用 var arrayOfArray = [[String?]](repeating: [], count: 10) 我知道Kotlin有listOf但是我不知道如何在一行中传递一个空列表的数组…

“模式匹配”不适用于Int子句(分支)

我在Kotlin有一段代码(我开始学习): package io.shido.learning import java.time.Instant fun typeCheck(any: Any): Any = when (any) { (any is Int && any “(small) integer” is Int -> “integer” is Double -> “double” is String -> “string” else -> “another Any” } fun main(args: Array) { println(“type check for: 5 (${typeCheck(5)})”) println(“type check for: 20 (${typeCheck(20)})”) println(“type check for: 56.0 […]

Int :: class.javaPrimitiveType.kotlin引用不等于Int :: class.javaObjectType.kotlin

我认为情况2也应该是真实的。 这种行为是正确的吗? // CASE 1 Int::class.javaPrimitiveType!!.kotlin == Int::class.javaObjectType.kotlin // true // CASE 2 Int::class.javaPrimitiveType!!.kotlin === Int::class.javaObjectType.kotlin // false

等待服务被绑定使用协程

所以我有一个绑定到服务的方法。 fun bindService() { val intent = Intent(this, BluetoothService::class.java) bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE) } 里面的onCreate方法我使用这个代码: bindService() launch { delay(500L) service = serviceConnection.serviceBinder?.getService() as BluetoothService } 有没有更优雅的方式来等待服务被绑定比使用delay() ?

Android架构组件LiveData

我正在尝试使用架构组件来实现一个简单的应用程序。 我可以使用Retrofit2从RestApi服务获取信息。 我可以在各自的Recyclerview中显示信息,当我旋转手机时,一切都按照原样进行。 现在我想过滤一个新types的对象(通过字符串) 有人可以用ViewModel指导我一点,我不知道最好的做法是什么…我正在使用MVVM … 这是我的ViewModel: public class ListItemViewModel extends ViewModel { private MediatorLiveData mList; private MeliRepository meliRepository; /* Empty Contructor. * To have a ViewModel class with non-empty constructor, * I have to create a Factory class which would create instance of you ViewModel and * that Factory class has to implement ViewModelProvider.Factory […]