array: があり、次のコードでnotes: Array<KeyValueNote>?
Kotlin を使用しています。1.0.5-2
私が欲しい
if (notes != null) {
for (note in notes) {
// Put the note to the payload Json object only if the note is non-null.
payloadJson.put(note.key, note.value)
}
}
しかし、いくつかの変更点があります
// Alternative 1.
notes?.let {
it.takeWhile { it != null /** Inspection will note The condition 'it != null' is always true' in here**/ }.forEach { payloadJson.put(it.key, it.value) }
}
// Alternative 2.
notes?.takeWhile { it != null /** Inspection will note The condition 'it != null' is always true' in here**/ }?.forEach { payloadJson.put(it.key, it.value) }
// Alternative 3.
notes?.filterNotNull()?.forEach { payloadJson.put(it.key, it.value) }
私の質問
The condition 'it != null' is always true
代替案 1&2 に検査メモがあることがわかりますが、検査は正しいですか? の非 null アイテムのみを にnotes
配置できるようにしたいためpayloadJson
です。filterNotNull()?.
代替案 3 では、にSafe?
Call があることがわかります。、ソース コードを確認したため、結果filterNotNull()
が null になることはありませんが?
、そこを削除するとコンパイルに失敗します。