Equatable lives only in the Swift world, so you cannot extend it to the protocol that Objective-C will use. Attempting to do this results in error # 1
Protocols requiring Self (i.e., at least one method from a protocol declaration contains Self ) cannot be used as arguments for functions or for variable declarations only as arguments in a general sentence, for example. func doSomething<T: Option>(argument: T) .
Removing Equatable from the Option protocol declaration, and declaring == as common in Option will eliminate compilation errors. As for sorting, you can also overload the < operator and sort it through this operator (without having to execute Comparable ):
@objc protocol Option { var title: String { get } var enabled: Bool { get } var position: Int { get } } func ==<T: Option>(lhs: T, rhs: T) -> Bool { return lhs.position == rhs.position } func <<T: Option>(lhs: T, rhs: T) -> Bool { return lhs.position < rhs.position }
This allows you to transfer objects that comply with the protocol to UIKit , as well as compare them in your fast code.
class A: NSObject, Option { .. } class B: NSObject, Option { ... } let a = A() let b = B() a == b
One important note about the sort code described above: if you do not specify a type for c , then the compiler will display its type as [NSObject] , and the sort call will not compile due to the ambiguity of < . You need to explicitly declare c as [Option] in order to use the overloaded operator.
source share