Kotlin での Boolean - Int 変換 質問する

Kotlin での Boolean - Int 変換 質問する

Kotlin には boolean と int を変換する組み込みの方法はありませんか? よくあることですが、

true -> 1
false -> 0

そうでない場合、慣用的な方法は何ですか?

ベストアンサー1

ブール値の拡張関数は次のように記述できます。

fun Boolean.toInt() = if (this) 1 else 0

おすすめ記事