Sobes.tech
Junior — Middle

Kas saaksite selgitada kordamise (repeating) kontseptsiooni Swift keeles ja kuidas seda kasutatakse koodis?

sobes.tech AI

Vastus AI-lt

In Swift, there is no built-in concept of "repeating" as a keyword, but there is a repeat function in loops and a repeating parameter in some APIs.

For example, to create an array with repeated elements, the initializer is used:

let repeatedArray = Array(repeating: "Hello", count: 3)
print(repeatedArray) // ["Hello", "Hello", "Hello"]

There is also the repeat-while operator, which executes the loop at least once:

var i = 0
repeat {
    print(i)
    i += 1
} while i < 3

Thus, "repeating" is used to create repeated elements or in loops with repetition.