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

后端 未结 13 2249
遥遥无期
遥遥无期 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:17

    You could do something like this:

    10⨉{ print("loop") }
    

    Using a custom operator and an extension on Int:

    infix operator ⨉ // multiplication sign, not lowercase 'x'
    
    extension Int {
        static func ⨉( count:Int, block: () ->Void  ) {
            (0..

提交回复
热议问题