将使用除法的C样式循环转换为Swift 3 [英] Converting a C-style for loop that uses division for the step to Swift 3

查看:185
本文介绍了将使用除法的C样式循环转换为Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 对于var i = 128;我有这个循环,在div中减去一个整数。 i> = 1; i = i / 2 {
//做一些事情
}

C风格的for循环已被弃用,所以我怎样才能将其转换为Swift 3.0?

解决方案

MartinR的解决方案是非常通用和有用的并应该是你的工具箱的一部分。

另一种方法是重写你想要的:两个从7降到0的权力。


(0 ... 7).reversed()。map({1 <= $ 0}){
print(i)的

 
}


I have this loop, decrementing an integer by division, in 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?

解决方案

MartinR's solution is very generic and useful and should be part of your toolbox.

Another approach is to rephrase what you want: the powers of two from 7 down to 0.

for i in (0...7).reversed().map({ 1 << $0 }) {
    print(i)
}

这篇关于将使用除法的C样式循环转换为Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆