Swift 2.2 decrementing specific for loop in Swift 3

前端 未结 4 400
感情败类
感情败类 2021-01-27 14:13

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

4条回答
  •  佛祖请我去吃肉
    2021-01-27 14:35

    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)
    

提交回复
热议问题