我的任务是将 iOS 应用程序重构为 Swift 3。但是,有一个for
C 风格的循环,它不仅仅是向后循环数组(必须向后循环)。
这是一个示例代码。原理是一样的。
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:)
但我不能增加threeLetterWords
,因为在循环中增加它似乎很重要。有任何想法吗?