What is the shortest way to run same code n times in Swift?

后端 未结 13 2252
遥遥无期
遥遥无期 2021-02-01 00:58

I have a code that I need to run exactly n times in Swift. What is the shortest possible syntax for that?

I am currently using the for loop but

13条回答
  •  时光取名叫无心
    2021-02-01 01:06

    There are a lot of answers here, highlighting just how creative you can be, with Swift.

    I needed an array so I did this

    extension Int {
        func of(iteration: (Int) -> T) -> [T] {
            var collection = [T]()
            for i in 0.. [String] {
        return 4.of { "\($0) teletubby" }
    }
    

提交回复
热议问题