This question already has answers here:
How to iterate for loop in reverse order in swift?
(16 answers)
Closed 4 years ago.
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)
}
Related
This question already has answers here:
How to compare one value against multiple values - Swift
(8 answers)
Closed 2 years ago.
I want to ask if the generated random Number is 1 or 13 or 25. I cut ask something like:
if randomNumber == (1) || if randomNumber == (13) || if randomNumber == (25)
and that's works but its way to much code. I try to minimize my code.
I did try something like this:
if randomNumber == (1 || 13 || 25)
but this didn't worked.
You can convert them to a simple collection like an array or something and use its methods like:
if [1, 13, 25].contains(randomNumber)
More generally:
if (<#myArray#>.contains { <#condition#> }
This question already has an answer here:
How can I do a Swift for-in loop with a step?
(1 answer)
Closed 3 years ago.
I want to traverse an array with a stride. For example, I have an array [0,1,2,3,4,5,6,7,8,9].And I want to traverse this array with a stride of 3. The oc code likes below:
for (int i = 0; i < array.count; i += 3) {
}
How can I do that with swift.
You can use a for loop with stride to traverse the array
let testarray = [0,1,2,3,4,5,6,7,8,9]
for i in stride(from: 0, to: testarray.count, by: 3) {
print(testarray[i])
}
This question already has answers here:
For-In Loops multiple conditions
(5 answers)
Closed 5 years ago.
I have a C-style for loop that's no longer supported in Swift 3. It looks something like this:
for (var x = 0; x < foo.length && x < bar.length; x++) {}
What's the equivalent of this that's available now in Swift 3?
if you only need to check the two limits, how about this
for x in 0..<min(foo.length, bar.length)
{
// do stuff
}
This question already has answers here:
Is there a way to instantly generate an array filled with a range of values in Swift?
(4 answers)
Closed 6 years ago.
Is there a shorter way to create an array of numbers spanning a range in swift?
Right now, I'm using this:
var arrayOfInts = [UInt32]()
for number in 1...100 {
arrayOfInts.append(number)
}
Is there a one-line way of doing it?
var arrayOfInts = Array(1...100)
Playground Output
Is this short enough?
let array = Array(1...100)
Try like this
var z = [Int](1...100)
print(z)
DEMO
This question already has answers here:
Fix warning "C-style for Statement is deprecated" in Swift 3
(4 answers)
Decrement index in a loop after Swift C-style loops deprecated
(5 answers)
Closed 6 years ago.
for (int i = n-2; i >= 0; --i)
{
....
}
the automatic translation of the above to swift 3 syntax is this
for i in n-2 ... 0
{
}
this doesn't work, because n could be 1, in the c syntax, this is valid, the loop won't be triggered,
but in the swift 3 syntax, this will cause runtime error.
for i in stride(from: n-2, through: 0, by: -1) {
}
for i in (0 ... n-2).reversed() {
}