How do I implement custom collection types?

Custom collection types in Swift allow developers to create tailored data structures that can enhance code readability and maintainability. These collections can encapsulate functionality specific to the collection's purpose, making it easier to manage and manipulate data effectively.
swift, custom collection, data structure, programming, Swift collections
struct CustomCollection<Element> {
    private var items: [Element] = []

    mutating func add(_ item: Element) {
        items.append(item)
    }

    func getAll() -> [Element] {
        return items
    }
}

// Example of using CustomCollection
var myCollection = CustomCollection<String>()
myCollection.add("Hello")
myCollection.add("World")
print(myCollection.getAll()) // Output: ["Hello", "World"]
    

swift custom collection data structure programming Swift collections