I have the following code:
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) }
If I call this like:
var list : ArrayList<Person>? = p1.findSomeLikeThis() for (p2 in list) { p2.delete() p2.commit() }
It would give me the error:
For-loop range must have an 'iterator()' method
Am I missing something here?
Your ArrayList
is of nullable type. So, you have to resolve this. There are several options:
for (p2 in list.orEmpty()) { ... }
or
list?.let { for (p2 in it) { } }
or you can just return an empty list
public fun findSomeLikeThis(): List<T> //Do you need mutable ArrayList here? = (Db4o.objectContainer()!!.queryByExample<T>(this as T) as Collection<T>)?.toList().orEmpty()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With