This question already has answers here:
How to iterate for loop in reverse order in swift?
(16 answers)
Reverse Range in Swift
(7 answers)
Closed 4 years ago.
for var i=count-2; i>=0; --i
{
if let nextControlPoint = firstControlPoints[i+1]
{
let controlPointX = (rhsArray[i].x.f - c[i] * nextControlPoint.x.f)/b[i]
let controlPointY = (rhsArray[i].y.f - c[i] * nextControlPoint.y.f)/b[i]
}
z += 1
}
You can write something like this
if count > 1 {
for i in (0...count-2).reversed() {
print(i)
}
}
The IF statement is needed because we cannot create a range where the first element is lower than the last one.
This question already has answers here:
Reverse Range in Swift
(7 answers)
Closed 4 years ago.
In the default case in this switch statement, I'm trying to iterate backwards in a for loop, there are examples of how to do this when working with Int's but I haven't found any with variables.
func arrayLeftRotation(myArray: [Int], d:Int) {
var newArray = myArray
switch d {
case 1:
let rotationValue = newArray.removeLast()
newArray.insert(rotationValue, at: 0)
default:
let upperIndex = newArray.count - 1
let lowerIndex = newArray.count - d
for i in lowerIndex...upperIndex {
let rotationValue = newArray.remove(at: i)
newArray.insert(rotationValue, at: 0)
}
}
print(newArray)
}
So I wish to count down from upperIndex to lowerIndex
You cannot do that with a for ... in ... statement. When using a for ... in ... statement, both the index variable and the range are immutable and you have no control over how the range is iterated through.
However, there are several alternatives you can use, such as while loops, strides and recursion.
Example for how to iterate over a range in descending order using a stride:
stride(from: upperIndex, through: lowerIndex, by: -1).forEach({ index in
let rotationValue = newArray.remove(at: index)
newArray.insert(rotationValue, at: 0)
})
This question already has answers here:
Shuffle array swift 3
(4 answers)
Closed 6 years ago.
I have:
extension MutableCollection where Index == Int { // shuffle elements of self in place
mutating func shuffleInPlace() {
if count < 2 { return } // empty and single-element collections don't shuffle
for i in 0 ..< count - 1 {
let j = Int( arc4random_uniform( UInt32( count - i ) ) ) + i
guard i != j else { continue }
swap( &self[ i ], &self[ j ] )
...
...
and I'm getting the error:
Binary operator Binary operator '..<' cannot be applied to operands of type 'Int' and 'Self.IndexDistance'
Does anyone know how to rectify this?
Try this instead, wrap count -1 in parenthesis :
for i in 0 ..< (count - 1)
This question already has answers here:
How to iterate for loop in reverse order in swift?
(16 answers)
Closed 6 years ago.
I am trying to do convert this C code:
int i;
for (i = 9; i >= 0; i--) {
}
in Swift 3, but I am not sure how to do it.
I know that if if I want to do it in ascending order if i, I can simply write:
for i in 0..<10 {
}
But how do I do it in descending order of i?
Thanks in advance!
Two ways:
// reverse a range
for i in (0...9).reversed() {
// ...
}
// use stride
for i in stride(from: 9, through: 0, by: -1) {
// ...
}
for i in (0 ..< 10).reversed() {
}
How would you express a decrementing indexed loop in Swift 3.0, where the syntax below is not valid any more?
for var index = 10 ; index > 0; index-=1{
print(index)
}
// 10 9 8 7 6 5 4 3 2 1
Here is an easier (and more Swifty) approach.
for i in (0 ..< 5).reversed() {
print(i) // 4,3,2,1,0
}
let array = ["a", "b", "c", "d", "e"]
for element in array.reversed() {
print(element) // e,d,c,b,a
}
array.reversed().forEach { print($0) } // e,d,c,b,a
print(Array(array.reversed())) // e,d,c,b,a
C-style loops with a fixed increment or decrement can be replaced
by stride():
for index in 10.stride(to: 0, by: -1) {
print(index)
}
// 10 9 8 7 6 5 4 3 2 1
Use stride(to: ...) or stride(through: ...) depending on whether
the last element should be included or not.
This is for Swift 2. The syntax changed (again) for Swift 3, see
this answer.
From swift 3.0, The stride(to:by:) method on Strideable has been replaced with a free function, stride(from:to:by:)
for index in stride(from: 10, to: 0, by: -1) {
print(index)
}
// You can also use stride condition like
// {Some Value}.stride(to: 0, by: -1)
// for index in 10.stride(to: 0, by: -1) { }
You can use stride method:
10.stride(through: 0, by: -1).forEach { print($0) }
or classic while loop.
If you still want to use this C-style loop, here is what you need:
let x = 10
infix operator ..> { associativity left }
func ..>(left: Int, right: Int) -> StrideTo<Int> {
return stride(from: left, to: right, by: -1)
}
for i in x..>0 {
print(i)
}