我有以下代码:
public fun findSomeLikeThis(): ArrayList<T>? {
val result = Db4o.objectContainer()!!.queryByExample<T>(this as T) as Collection<T>
if (result == null) return null
return ArrayList(result)
}
Run Code Online (Sandbox Code Playgroud)
如果我称之为:
var list : ArrayList<Person>? = p1.findSomeLikeThis()
for (p2 in list) {
p2.delete()
p2.commit()
}
Run Code Online (Sandbox Code Playgroud)
它会给我错误:
For循环范围必须具有'iterator()'方法
我在这里错过了什么吗?
nai*_*ixx 46
你ArrayList
是可空的类型.所以,你必须解决这个问题.有几种选择:
for (p2 in list.orEmpty()) { ... }
Run Code Online (Sandbox Code Playgroud)
要么
list?.let {
for (p2 in it) {
}
}
Run Code Online (Sandbox Code Playgroud)
或者你可以只返回一个空列表
public fun findSomeLikeThis(): List<T> //Do you need mutable ArrayList here?
= (Db4o.objectContainer()!!.queryByExample<T>(this as T) as Collection<T>)?.toList().orEmpty()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15020 次 |
最近记录: |