Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: For-loop range must have an 'iterator()' method

Tags:

kotlin

If I use var charSet = 1..10 it works, but when I am using the below code, I'm getting this error.

var charSet = "A".."Z"
for (value in charSet) {    
  println("$value")  
}

For-loop range must have an 'iterator()' method.

Please let me know how to resolve this.

like image 669
Venkatesh_J Avatar asked Feb 18 '26 10:02

Venkatesh_J


1 Answers

charSet is a ClosedRange<String> and that type doesn't have iterator() function (but ex. IntRange does). You can either change your range to 'A'..'Z' or create an extension function ClosedRange<String>.iterator() which would return a Iterator. The error should then go away

like image 136
Mibac Avatar answered Feb 21 '26 15:02

Mibac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!