I'm trying to implement the Comparable protocol in Swift, but the compiler doesn't like any of my attempts to overload the < operator. I've checked the Apple documents and all the SO posts, but none of them even compile. Xcode gives me this warning:
Consecutive declarations on a line must be separated by ';'
and it keeps recommending me to insert a semicolon after the less than symbol. Any insight on what I'm doing wrong is appreciated.
class SomeClass: NSObject, Equatable, Comparable{
var number: UInt32!
override init()
{
super.init()
self.number = arc4random()
}
func == (lhs: SomeClass, rhs: SomeClass) -> Bool
{
return true
}
func < (lhs: SomeClass, rhs: SomeClass) -> Bool
{
return true
}
}
You see this error, because operators have to be overloaded outside the class definition, e.g. move
func == (lhs: SomeClass, rhs: SomeClass) -> Bool
{
return true
}
func < (lhs: SomeClass, rhs: SomeClass) -> Bool
{
return true
}
outside your class definition and it will work (except for that they do not return the proper result with this implementation).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With