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

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

    In Swift, what you have is the shortest syntax for performing a loop operation.

    Swift provides two kinds of loop that perform a set of statements a certain number of times:

    The for-in loop performs a set of statements for each item in a sequence.

    The for loop performs a set of statements until a specific condition is met.

    If you want to run it infinite times, well try using a while.

提交回复
热议问题