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

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

    ONLY 5 CHARACTERS (not including n or code)

    r(){}
    

    If you're just testing things and need a REALLY short line, try this. Emphasis on using this for testing, not in production, because no one will know what is going on without documentation.

    define this somewhere globally

    func r(_ n : UInt, _ c: @escaping () -> Void) { for _ in 0..

    call this when you want to run it

    r(5) { /*code*/ }
    

提交回复
热议问题