【发布时间】:2018-05-31 02:51:17
【问题描述】:
我有一个接受value: String? 的方法。该方法的目的是将字符串值转换为正确的表示形式。例如:
"true" -> a Boolean that has a true value
"20" -> an Int that contains 20
"1223.2" -> a Dobule
在 Koltin 中实现此结果的最有效/最简洁的方法是什么?
更新
这是我当前的代码:
private fun insertRealType(map: WritableMap, key: String, value: String?) {
val test = getValueType(value)
when (test) {
is String -> map.putString(key, test)
is Boolean -> map.putBoolean(key, test)
is Int -> map.putInt(key, test)
is Double -> map.putDouble(key, test)
else -> map.putNull(key)
}
}
private fun getValueType(value: String?): Any {
var returnValue: Any?
returnValue = value?.toDoubleOrNull()
if (returnValue != null)
return returnValue
returnValue = value?.toIntOrNull()
if (returnValue != null)
return returnValue
// ETC......
}
【问题讨论】:
-
到目前为止你的代码是什么样的?这不是代码编写服务。
-
@zsmb13 这是我一直在研究的示例代码 sn-p
标签: kotlin