Swift 2.2特定于Swift 3中循环的递减

Swift 2.2特定于Swift 3中循环的递减,swift,for-loop,refactoring,operators,stride,Swift,For Loop,Refactoring,Operators,Stride,我的任务是将iOS应用程序重构为Swift 3。然而,C风格的for循环不仅仅是向后循环数组(必须向后循环) 这是一个示例代码。原理是一样的 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; --

我的任务是将iOS应用程序重构为Swift 3。然而,C风格的
for
循环不仅仅是向后循环数组(必须向后循环)

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

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:)
但是我不能增加
threeletwords
,因为在循环中增加它似乎很重要。有什么想法吗?

您可以使用反向数组索引,并为字符计数添加where子句:

//for var i = array.count-1; i >= 0 && array[i].characters.count == 3; --i, ++threeLetterWords { }

for i in stride(from: (array.count-1), through: 0, by: -1) {
    threeLetterWords += 1

    if (array[i]?.characters.count == 3) {
        break
    }
}
let array = ["hello", "world", nil, "foo", nil, "bar", "Peter Griffin"]
var threeLetterWords = 0

for index in array.indices.reversed() where array[index]?.characters.count == 3 {
    threeLetterWords += 1
}

print("Found words: \(threeLetterWords)") // should say `Found words: 2`

您可以使用数组索引,并为字符数添加where子句:

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

for index in array.indices.reversed() where array[index]?.characters.count == 3 {
    threeLetterWords += 1
}

print("Found words: \(threeLetterWords)") // should say `Found words: 2`

您的代码没有计算数组中3个字母的单词数。它正在计算数组末尾的3个字母的单词数。对于示例输入数组,它将返回
0

当C风格的for循环非常复杂时,最终的回退解决方案是将其转换为while循环。任何C风格的for循环都可以机械地转换成等价的while循环,这意味着即使您不完全理解它在做什么,您也可以这样做

此for循环:

相当于:

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`
因此,您的代码相当于:

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`

下面是如何使用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)

您的代码没有计算数组中3个字母的单词数。它正在计算数组末尾的3个字母的单词数。对于示例输入数组,它将返回
0

当C风格的for循环非常复杂时,最终的回退解决方案是将其转换为while循环。任何C风格的for循环都可以机械地转换成等价的while循环,这意味着即使您不完全理解它在做什么,您也可以这样做

此for循环:

相当于:

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`
因此,您的代码相当于:

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`

下面是如何使用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)

这里的每个人都不必要地把这件事复杂化了

let words = ["hello", "world", nil, "foo", nil, "bar", "Peter Griffin"]

var num3LetterWords = 0

for word in words.reversed() {
    if (word?.characters.count == 3) { num3LetterWords += 1 }
}

print(num3LetterWords)

这里的每个人都不必要地把这件事复杂化了

let words = ["hello", "world", nil, "foo", nil, "bar", "Peter Griffin"]

var num3LetterWords = 0

for word in words.reversed() {
    if (word?.characters.count == 3) { num3LetterWords += 1 }
}

print(num3LetterWords)


任何C样式的for循环都可以替换为while循环。您的代码计算数组末尾的3个字母的单词数。它将为您的测试数组返回0。我现在完全理解为什么删除了C-style for循环。@Alexander,你说得对。有一些非常邪恶的代码滥用C-style for循环。任何C-style for循环都可以替换为while循环。您的代码计算数组末尾的3个字母的单词数。它将为您的测试数组返回0。我现在完全理解为什么删除了C-style for循环。@Alexander,你说得对。有一些非常邪恶的代码滥用C风格进行循环。这是一种不必要的复杂方式,用于执行
打印(words.count)
@vacawama hahaha我忘记在if语句中复制,已修复。您需要打开:
word?.characters.count==3
。这会计算3个字母的单词,但它并不等同于OP的原始代码,而OP的原始代码实际上会计算数组末尾3个字母的单词数。@vacawama我的目标是重现代码的“精神”,而不是代码的“单词”,因为我怀疑OP在制作这个糟糕的例子时犯了一个错误,我相信他们问题的实质是如何将一个复杂的C风格for循环转换成Swift 3中的等价物,因为他们的问题承认他们有一些复杂的例子,他们不想与大家分享,并制作了这个例子作为替代。考虑到我们不知道他们的循环有多复杂,我想我应该向他们展示最后一个回退场景,即将其转换为while循环,特别是如果您不理解原始代码的话。这是一种不必要的复杂方式,可以执行
打印(words.count)
@vacawama哈哈哈,我忘了在if语句中复制,已修复。您需要展开:
word?.characters.count==3
。这会计算3个字母的单词,但它并不等同于OP的原始代码,而OP的原始代码实际上会计算数组末尾3个字母的单词数。@vacawama我的目标是重现代码的“精神”,而不是代码的“单词”,因为我怀疑OP在制作这个糟糕的例子时犯了一个错误,我相信他们问题的实质是如何将一个复杂的C风格for循环转换成Swift 3中的等价物,因为他们的问题承认他们有一些复杂的例子,他们不想与大家分享,并制作了这个例子作为替代。考虑到我们不知道他们的循环有多复杂,我想我应该向他们展示最后一个回退场景,即将其转换为while循环,特别是如果您不理解原始代码的话。