InitializerSwift

init(repeating:count:)

Creates a new collection containing the specified number of a single, repeated value.

init(repeating repeatedValue: Self.Element, count: Int)

Parameters

repeatedvalue

The element to repeat.

count

The number of times to repeat the value passed in the repeating parameter. count must be zero or greater.

Overview

Here’s an example of creating an array initialized with five strings containing the letter Z.

let fiveZs = Array(repeating: "Z", count: 5)
print(fiveZs)
// Prints "["Z", "Z", "Z", "Z", "Z"]"