Swift 2.2在swift 3中为循环递减 [英] Swift 2.2 decrementing specific for loop in Swift 3
问题描述
我有任务将一个iOS应用重构为Swift 3.但是,对于循环来说,有一个 这是一个示例代码。原则是一样的。 我用 您的代码不包括数组中的3个字母的单词数。它正在计数数组末尾的3个字母的单词数。它会返回 当一个C风格 for 循环非常复杂,最后的回退解决方案是将它翻译为 while 循环。任何C风格的 for 循环都可以机械地转换成等效的 while 循环,这意味着即使您不完全明白它在做什么,也可以做到这一点。 / p> 这个 for 循环: 相当于: 因此,您的代码相当于: 是如何使用 for 循环和 guard 来完成代码的等效操作: I have the task to refactor an iOS app to Swift 3. However, there is a This is a sample code. The principle is the same. I tried with 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 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: is equivalent to: So, your code is equivalent to:
Here is how to use a for loop and guard to do the equivalent of your code:
这篇关于Swift 2.2在swift 3中为循环递减的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!循环,它不仅仅是循环一个数组向后(必须向后)。
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
,因为在循环中增加它似乎很重要。任何想法?
0
为您的示例输入数组。
初始化;条件;增量{
// 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`
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)
for
loop, in C-style, that does more than just looping an array backwards (it's mandatory to be backwards).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`
stride(from:through:by:)
but I can't increment threeLetterWords
as it seems important to increment it in the loop. Any ideas?0
for your sample input array.for initialization; condition; increment {
// body
}
initialization
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("Found words: \(threeLetterWords)") // says `Found words: 0`
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)