神刀安全网

Swift Alternatives to C-style for-loops

Starting in Swift 3.0, C-style for-loops will be gone from Swift! You can read the full Swift Evolution proposal here .

Last week, I talked to an iOS developer who was upset by this (it is a long-held habit after all!) and was confused by what to use as an alternative. @twostraws did a great write-up on the new Swift 2.2 features and what to use instead, so I’m going to write it down here for my own (and your) reference.

Here are the more-readable Swift alternatives.

Looping n times

/* 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 */   // Instead of this: for var i = 0; i < 10; i++ {     print(i) }   // use this: for i in 1...10 {     print(i) } 

Looping n times in reverse

/* 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 */   // instead of this for var i = 10; i > 0; i-- {     print(i) }   // use this for i in (1...10).reverse() {     print(i) } 

Looping with Stride

/* 0, 2, 4, 6, 8 */   // instead of this for var i = 0; i < 10; i += 2 {     print(i) }   // use this for i in 0.stride(to: 10, by: 2) {     print(i) } 

Looping through Array Values

let someNumbers = [2, 3, 45, 6, 8, 83, 100]   /* 2, 3, 45, 6, 8, 83, 100 */   // instead of this for var i = 0; i < someNumbers.count; i++ {     print(someNumbers[i]) }   // use this for number in someNumbers {     print(number) } 

Reverse Looping through Array Values

let someNumbers = [2, 3, 45, 6, 8, 83, 100]   /* 100, 83, 8, 6, 45, 3, 2 */   // instead of this for var i = someNumbers.count - 1; i >= 0; i-- {     print(someNumbers[i]) }   // use this for numberin someNumbers.reverse() {     print(number) } 

Looping With Through Array with Index

let someNumbers = [2, 3, 45, 6, 8, 83, 100]   /* 1: 2 2: 3 3: 45 4: 6 5: 8 6: 83 7: 100 */   // intead of this for var i = 0; i < someNumbers.count; i++ {     print("/(i + 1): /(someNumbers[i])") }   // use this for (index, number) in someNumbers.enumerate() {     print("/(index + 1): /(number)") } 

Conclusion

I tend to use pretty simple for-loops, so these seem good enough for me. However, if you have another configuration that I’m missing, let me know in the comments!

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Swift Alternatives to C-style for-loops

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮