I have a class AAA which contains an generic array.
Since Item could be not Equatable, so I do not code it as class AAA<Item: Equatable>
.
I would like to add an remove function in class AAA which is available when Item is Equatable. By calling this function, it will call a function in the Array Extension.
Since Item is not Equatable by default, the following code is not able to compile.
class AAA<Item>
{
var items = [Item]()
func remove(item: Item)
{
items.remove(object: item)
}
}
extension Array where Element: Equatable
{
// Remove first collection element that is equal to the given `object`:
mutating func remove(object: Element)
{
if let index = index(of: object)
{
remove(at: index)
}
}
}
In the next step, I try to convert the items and item into Equatable. However, I cannot find any way to check and cast the items array into a array with Equatable elements.
func remove<ItemEquatable: Item> (item: ItemEquatable) where ItemEquatable: Equatable
{
items.remove(object: item)
}
Any idea or suggestion? Thanks