Using find () in an array of protocol types

When I create an array of protocol types, how would I get the index of an object in this array? I tried the following:

protocol aProtocol {
    func doSomething()
}

class aClass: aProtocol, Equatable {
    var aProperty = "test"

    func doSomething() {

    }
}

func == (lhs: aClass, rhs: aClass) -> Bool {
    return lhs.aProperty == rhs.aProperty
}

var testArray = aProtocol[]()
let testObject = aClass()

testArray += testObject

find(testArray, testObject)

In this case, I get the message "Unable to convert expression type" $ T4? "to enter the error" aClass ".

Looking for the signature of the find () method, we find that the element must implement Equatable (which is why I overload the == operator above):

func find<C : Collection where C.GeneratorType.Element : Equatable>(domain: C, value: C.GeneratorType.Element) -> C.IndexType?

Does anyone have an idea of ​​what I'm doing wrong, or if this scenario is possible? Thank.

+4
source share
2 answers

find() , Equatable, Equatable , find() aProtocol .

,

protocol aProtocol : Equatable { ... }

Equatable aClass.

+2

Source: https://habr.com/ru/post/1546699/


All Articles