kotlinことはじめ
kotlinでアプリを書き出すようになったので、勉強がてらメモ。
static method
基本staticは無いらしい。class objectで記述するみたい。
いつものアレをconvertしたらこうなった
public class SomeFragment : Fragment() {
companion object {
public fun getInstance(): Fragment {
return Fragment()
}
}
}
val var
swiftのletにvalが相当する。変更不可能な値。 varは変更可能な値。
getBytes()
Stringに生えているgetBytes()はdeprecatedになっていた。 代わりにtoByteArray()をつかえとのこと
ByteArray
Basic types - Kotlin - Confluence
Arrayで配列を表現するみたい。 primitive型も対応する型が用意されているみたい。
Test
src/test/kotlin/PKG/HogeTest.kt
package hoge
import kotlin.test.assertTrue
import org.junit.Test
public class HogeTest {
Test fun hoge() {
assertTrue(true)
}
}
Annotationは@を外してつかうっぽい
butterknife
kotterknifeがあるっぽい
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
dependencies {
compile 'com.jakewharton:kotterknife:0.1.0-SNAPSHOT'
}
import butterknife.bindView
public class MapFragment : Fragment() {
val mMapView: MapView by bindView(R.id.map)
}
Parameter specified as non-null is null
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
というエラーがちょくちょく発生する。コレはoverrideしたjavaの関数にて、optionalを気にせずoverrideしてると呼び出し元の関数からnullが渡ってきてエラーが起きる。 java関数をoverride するときには、optionalかどうかを厳密に考えよう・・・。
traitはdeprecated
かわりにinterfaceをつかう。
data class
data public class Hoge(val name: String, val createdAt: String)
みたいな感じでデータ宣言できる。
Signleton
object Singleton {
fun hello() {
println("Hello")
}
}
object declarationで簡単にかけるっぽい。