How to create mock arrays with Array(repeating:count:)


Greetings, traveler!

Sometimes, we need to create some mock data. It looks tricky at first glance when we talk about arrays with extensive data. However, there is an API in Swift that can help us create such an array in a single line of code.

Check out this example:

let stringArray = [String](repeating: "", count: 888)

This initializer will create an array with the desired count of similar elements. We can use it with different types, including custom ones.

struct Model {
    let name: String
}

let modelArray = [Model](repeating: .init(name: "Model Name"), count: 888)

Conclusion

With this API, creating mock data becomes a bit easier.