Swift 2.2 decrementing specific for loop in Swift 3

有些话、适合烂在心里 提交于 2019-12-20 06:29:45

问题


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?


回答1:


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)



回答2:


//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
    }
}



回答3:


You can use your array indices reversed and add a where clause for the characters count:

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`



回答4:


Everybody here is very unnecessarily complicating this.

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)


来源:https://stackoverflow.com/questions/42661748/swift-2-2-decrementing-specific-for-loop-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!