Hello,
Having a very strange issue. I want to iterate over strings in a mutable list. But getting the error message:
“For-loop range must have an ‘iterator()’ method”.
A second Variant:
Hello,
Having a very strange issue. I want to iterate over strings in a mutable list. But getting the error message:
“For-loop range must have an ‘iterator()’ method”.
A second Variant:
myList
is a mutable property and it can be changed after the null check (as the second message clearly tells), that’s why compiler doesn’t allow smart-cast in the subsequent usages.
Therefore myList
remains nullable, so one can’t invoke on it the size
property or the iterator()
function required for the for
-loop convention.
You can capture it in a local val
-variable and then check that for null. For a local variable compiler can prove that it cannot be changed after the check.
Thanks !! Makes total sense now.
I feel foolish.
It would make sense to use the extension List?.orEmpty()
If the list is null it will return an empty List