Swift 2.2在swift 3中为循环递减 [英] Swift 2.2 decrementing specific for loop in Swift 3

查看:182
本文介绍了Swift 2.2在swift 3中为循环递减的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有任务将一个iOS应用重构为Swift 3.但是,对于循环来说,有一个循环,它不仅仅是循环一个数组向后(必须向后)。

这是一个示例代码。原则是一样的。

  let array = [hello,world,nil,foo,nil, bar,Peter Griffin] 
var threeLetterWords = 0
for var i = array.count-1; i> = 0&&数组[i] .characters.count == 3; --i,++ threeLetterWords {}
print(找到的单词:\(threeLetterWords))//应该说是`Found words:2`

我用 stride(from:through:by:)尝试过,但是我不能增加 threeLetterWords ,因为在循环中增加它似乎很重要。任何想法?

解决方案

您的代码不包括数组中的3个字母的单词数。它正在计数数组末尾的3个字母的单词数。它会返回 0 为您的示例输入数组。



当一个C风格 for 循环非常复杂,最后的回退解决方案是将它翻译为 while 循环。任何C风格的 for 循环都可以机械地转换成等效的 while 循环,这意味着即使您不完全明白它在做什么,也可以做到这一点。 / p>

这个 for 循环:

 初始化;条件;增量{
// body
}

相当于:

 初始化
while condition {
// body
increment
}

因此,您的代码相当于:

  let array = [hello,world,nil,foo,nil,bar,Peter Griffin] 
var threeLetterWords = 0

var i = array.count - 1
while i> = 0&& array [i]?。characters.count == 3 {
i - = 1
threeLetterWords + = 1
}
print(找到的单词:\(threeLetterWords)) //说:找到的单词:0`






是如何使用 for 循环和 guard 来完成代码的等效操作:

  let array = [hello,world,nil,foo,nil,bar,Peter Griffin] 
var num3LetterWords = 0

for word in array.reversed(){
guard word?.characters.count == 3 else {break}
num3LetterWords + = 1
}

print( num3LetterWords)


I have the task to refactor an iOS app to Swift 3. However, there is a for loop, in C-style, that does more than just looping an array backwards (it's mandatory to be backwards).

This is a sample code. The principle is the same.

let array = ["hello", "world", nil, "foo", nil, "bar", "Peter Griffin"]
var threeLetterWords = 0
for var i = array.count-1; i >= 0 && array[i].characters.count == 3; --i, ++threeLetterWords { }
print("Found words: \(threeLetterWords)") // should say `Found words: 2`

I tried with stride(from:through:by:) but I can't increment threeLetterWords as it seems important to increment it in the loop. Any ideas?

解决方案

Your code isn't counting the number of 3-letter words in the array. It is counting the number of 3-letter words at the end of the array. It will return 0 for your sample input array.

When a C-style for loop is very complex, the final fallback solution is to translate it to a while loop. Any C-style for loop can be mechanically converted into an equivalent while loop, which means you can do it even if you don't fully understand what it is doing.

This for loop:

for initialization; condition; increment {
    // body
}

is equivalent to:

initialization
while condition {
    // body
    increment
}

So, your code is equivalent to:

let array = ["hello", "world", nil, "foo", nil, "bar", "Peter Griffin"]
var threeLetterWords = 0

var i = array.count - 1
while i >= 0 && array[i]?.characters.count == 3 {
    i -= 1
    threeLetterWords += 1
}
print("Found words: \(threeLetterWords)") // says `Found words: 0`


Here is how to use a for loop and guard to do the equivalent of your code:

let array = ["hello", "world", nil, "foo", nil, "bar", "Peter Griffin"]
var num3LetterWords = 0

for word in array.reversed() {
    guard word?.characters.count == 3 else { break }
    num3LetterWords += 1
}

print(num3LetterWords)

这篇关于Swift 2.2在swift 3中为循环递减的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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