Search code examples
swiftgenericsfunctional-programming

How to make a constrained extension of Collection generic in Swift?


Context

I have a class Profile which conforms to protocol Entity. I am also adding a search method to all Swift Collections where the Element is a Profile.

However, I would like to make this generic to support not only Profile but every object conforming to Entity.


Code

protocol Entity {
    var name: String { get }
}

class Profile: Entity { ... }

extension Collection where Element == Profile {
    func searched(with search: String) -> [Profile] {
        guard !(search.isEmpty) else { return self }
        return self.filter { ($0.name.lowercased().contains(search.lowercased())) }
    }
}

Question

How can I make the search method generic to support all objects conforming to Entity?


Solution

  • first, you should change the extension to the below code

    extension Collection where Iterator.Element: Entity
    

    next, change the return type of the method to

    func searched(with search: String) -> [Iterator.Element]
    

    Finally, the extension can be like this:

    extension Collection where Iterator.Element: Entity {
            func searched(with search: String) -> [Iterator.Element] {
                guard !(search.isEmpty) else { return [Iterator.Element]() }
                return self.filter { ($0.name.lowercased().contains(search.lowercased())) }
            }
    }