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

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

    Sticking with a for loop - you could extend Int to conform to SequenceType to be able to write:

    for i in 5 { /* Repeated five times */ }
    

    To make Int conform to SequenceType you'll could do the following:

    extension Int : SequenceType {
        public func generate() -> RangeGenerator {
            return (0..

提交回复
热议问题