【发布时间】:2016-12-28 19:54:51
【问题描述】:
我在 Java 中有这段代码
return mFingerprintManager.hasEnrolledFingerprints() &&
createKey(DEFAULT_KEY_NAME, true) &&
initCipher(mCipher, DEFAULT_KEY_NAME);
我已将其转换为 Kotlin,如下所示
return mFingerprintManager.hasEnrolledFingerprints() &&
createKey(DEFAULT_KEY_NAME, true) &&
if (mCipher != null) {
mCipher?.apply { initCipher(this, DEFAULT_KEY_NAME) }
return true
} else {
return false
}
有没有更好的方法来编写 Kotlin 代码,使其更简洁?变量 mCipher 定义为
private var mCipher: Cipher? = null
在班级层面。
【问题讨论】:
-
为什么 Java 代码在 Kotlin 中不能按原样工作?
initCipher不返回Boolean吗? -
问题在于 Kotlin 抱怨 mCipher 可能在 null 检查和 initCipher 中的访问之间发生了变化。
-
很高兴看到
initCipher的签名