List comprehension in Swift

前端 未结 8 2196
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 13:53

The language guide has revealed no trace of list comprehension. What\'s the neatest way of accomplishing this in Swift? I\'m looking for something similar t

8条回答
  •  时光说笑
    2020-12-07 14:41

    One way would be :

    var evens: Int[]()
    for x in 0..<10 {
        if x%2 == 0 {evens += x} // or evens.append(x)
    }
    
    • Range operators
    • Arrays

提交回复
热议问题