I have a Swift for loop defined like this:
for i in 20...1 {
array.append(i)
}
But I get a crash with message
Thread 1: Fatal error: Can't form Range with upperBound < lowerBound
What is the fix?
I have a Swift for loop defined like this:
for i in 20...1 {
array.append(i)
}
But I get a crash with message
Thread 1: Fatal error: Can't form Range with upperBound < lowerBound
What is the fix?
You need to reverse the range:
for i in (1...20).reversed() {
array.append(i)
}
You cannot loop in reverse order like that, if you want you can try this :
for i in stride(from: 20, through: 1, by: -1) {
array.append(i)
}