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

后端 未结 13 2363
遥遥无期
遥遥无期 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 01:23

    you could use functional programming on a range instead of a loop, for shorter and "nicer" syntax for example

    (0..

    Other answers mention defining your own syntax for that. So - that can be fine for a tiny personal project, or as a learning experience. But defining your own syntax for something so trivial and basic in a large project would be maintenance and readability hell.

提交回复
热议问题