I have this loop, decrementing an integer by division, in Swift 2.
我有这个循环,在Swift 2中按分区递减整数。
for var i = 128; i >= 1 ; i = i/2 {
//do some thing
}
The C-style for loop is deprecated, so how can I convert this to Swift 3.0?
不推荐使用C风格的for循环,那么如何将其转换为Swift 3.0呢?
3 个解决方案
#1
2
MartinR's solution is very generic and useful and should be part of your toolbox.
MartinR的解决方案非常通用且有用,应该是您工具箱的一部分。
Another approach is to rephrase what you want: the powers of two from 7 down to 0.
另一种方法是改写你想要的东西:2的幂从7下降到0。
for i in (0...7).reversed().map({ 1 << $0 }) {
print(i)
}
#2
6
Quite general loops with a non-constant stride can be realized with sequence
:
具有非常数步幅的相当一般循环可以通过序列实现:
for i in sequence(first: 128, next: { $0 >= 2 ? $0/2 : nil }) {
print(i)
}
-
Advantages: The loop variable
i
is a constant and its scope is restricted to the loop body. - 优点:循环变量i是常量,其范围仅限于循环体。
-
Possible disadvantages: The terminating condition must be adapted (here:
$0 >= 2
instead ofi >= 1
), and the loop is always executed at least once, for the first value. - 可能的缺点:必须调整终止条件(此处:$ 0> = 2而不是i> = 1),并且对于第一个值,循环始终至少执行一次。
One could also write a wrapper which resembles the C-style for loop more closely and does not have the listed disadvantages (inspired by Erica Sadun: Stateful loops and sequences):
人们还可以编写一个类似于C风格for循环的包装器,并且没有列出的缺点(受Erica Sadun启发:有状态循环和序列):
public func sequence<T>(first: T, while condition: @escaping (T)-> Bool, next: @escaping (T) -> T) -> UnfoldSequence<T, T> {
let nextState = { (state: inout T) -> T? in
guard condition(state) else { return nil }
defer { state = next(state) }
return state
}
return sequence(state: first, next: nextState)
}
and then use it as
然后用它作为
for i in sequence(first: 128, while: { $0 >= 1 }, next: { $0 / 2 }) {
print(i)
}
#3
3
I'll suggest that you should use a while
loop to handle this scenario:
我建议你应该使用while循环来处理这种情况:
var i = 128
while i >= 1
{
// Do your stuff
i = i / 2
}
#1
2
MartinR's solution is very generic and useful and should be part of your toolbox.
MartinR的解决方案非常通用且有用,应该是您工具箱的一部分。
Another approach is to rephrase what you want: the powers of two from 7 down to 0.
另一种方法是改写你想要的东西:2的幂从7下降到0。
for i in (0...7).reversed().map({ 1 << $0 }) {
print(i)
}
#2
6
Quite general loops with a non-constant stride can be realized with sequence
:
具有非常数步幅的相当一般循环可以通过序列实现:
for i in sequence(first: 128, next: { $0 >= 2 ? $0/2 : nil }) {
print(i)
}
-
Advantages: The loop variable
i
is a constant and its scope is restricted to the loop body. - 优点:循环变量i是常量,其范围仅限于循环体。
-
Possible disadvantages: The terminating condition must be adapted (here:
$0 >= 2
instead ofi >= 1
), and the loop is always executed at least once, for the first value. - 可能的缺点:必须调整终止条件(此处:$ 0> = 2而不是i> = 1),并且对于第一个值,循环始终至少执行一次。
One could also write a wrapper which resembles the C-style for loop more closely and does not have the listed disadvantages (inspired by Erica Sadun: Stateful loops and sequences):
人们还可以编写一个类似于C风格for循环的包装器,并且没有列出的缺点(受Erica Sadun启发:有状态循环和序列):
public func sequence<T>(first: T, while condition: @escaping (T)-> Bool, next: @escaping (T) -> T) -> UnfoldSequence<T, T> {
let nextState = { (state: inout T) -> T? in
guard condition(state) else { return nil }
defer { state = next(state) }
return state
}
return sequence(state: first, next: nextState)
}
and then use it as
然后用它作为
for i in sequence(first: 128, while: { $0 >= 1 }, next: { $0 / 2 }) {
print(i)
}
#3
3
I'll suggest that you should use a while
loop to handle this scenario:
我建议你应该使用while循环来处理这种情况:
var i = 128
while i >= 1
{
// Do your stuff
i = i / 2
}