Equatable
lives in the Swift world only, thus you cannot extend it to a protocol that will be used by Objective-C. Trying to do this results in error #1
Protocols that have a Self
requirement (i.e. at least one method from the protocol declaration contains Self
) cannot be used as arguments to functions, or to variable declarations, only as arguments to a generic clause, e.g. func doSomething<T: Option>(argument: T)
.
Removing Equatable
from the Option
protocol declaration, and declaring ==
as generic on Option
will solve the compile errors. As for sorting, you can also overload the <
operator, and sort via that operator (without needing to implement 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 pass objects that conform to the protocol to UIKit
, and to also compare them within your swift code.
class A: NSObject, Option { .. }
class B: NSObject, Option { ... }
let a = A()
let b = B()
a == b // compiles, and returns true if a and b have the same position
let c: [Option] = [a, b]
c.sort(<) // returns a sorted array by the `position` field
One important note regarding the sorting code above: if you don't specify the type for c
, then the compiler infers its type as [NSObject]
, and the sort
call will not compile due to ambiguity of the <
operator. You need to explicitly declare c
as [Option]
to take advantage of the overloaded operator.