我有以下方法可以获取Realm对象的新密钥。我希望能够将Class作为参数传递:
private fun getNextKeyForObject(myClass: Class<*>): Int {
mRealm?.let {
val maxId = it.where(myClass).max("id")
if (maxId != null) {
return it.where(myClass).max("id").toInt() + 1
}
}
return 0
}
我收到以下错误:
Type parameter bound for E in fun <E : RealmModel!> where(clazz: Class<E!>!): RealmQuery<E!>!
is not satisfied: inferred type CapturedTypeConstructor(*) is not a subtype of RealmModel!
答案 0 :(得分:2)
您需要指定generic upper bound
(请参阅https://kotlinlang.org/docs/reference/generics.html#upper-bounds):
private fun <T : RealmModel> getNextKeyForObject(myClass: Class<T>): Int {