I tried the example below from the Swift - Protocols manual page, but I am getting the compiler error:
error: type 'SkillLevel' does not conform to protocol 'Comparable':
enum SkillLevel: Comparable {
case beginner
case intermediate
case expert(stars: Int)
}
var levels = [SkillLevel.intermediate, SkillLevel.beginner,
SkillLevel.expert(stars: 5), SkillLevel.expert(stars: 3)]
for level in levels.sorted() {
print(level)
}
Xcode suggests to add the function below to make it to conform to Comparable:
static func < (lhs: SkillLevel, rhs: SkillLevel) -> Bool {
<#code#>
}
From the manual:
Swift provides a synthesized implementation of Comparable for enumerations that don’t have a raw value. If the enumeration has associated types, they must all conform to the Comparable protocol. To receive a synthesized implementation of <, declare conformance to Comparable in the file that contains the original enumeration declaration, without implementing a < operator yourself. The Comparable protocol’s default implementation of <=, >, and >= provides the remaining comparison operators.
I'm not sure if the enumeration in the code above has a raw value, but if it doesn't, shouldn't the code had compiled without the implementation of the function "<"?