本文介绍了成员运算符“=="必须至少有一个类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在qualityClass中实现Equatable协议,但显示成员运算符'=='必须至少有一个'eqaualityClass'类型的参数.有人能解释一下这里出了什么问题吗?
I am trying to implement Equatable protocol in equalityClass, but showing Member operator '==' must have at least one argument of type 'eqaualityClass' .can any one explain whats going wrong here?
protocol Rectangle: Equatable {
var width: Double { get }
var height: Double { get }
}
class eqaualityClass:Rectangle{
internal var width: Double = 0.0
internal var height: Double = 0.0
static func == <T:Rectangle>(lhs: T, rhs: T) -> Bool {
return lhs.width == rhs.width && rhs.height == lhs.height
}
}
推荐答案
你需要让你的 Rectangle 协议成为一个类.试试这个:
You need to make your Rectangle protocol a class. Try like this:
protocol Rectangle: class, Equatable {
var width: Double { get }
var height: Double { get }
}
class Equality: Rectangle {
internal var width: Double = 0
internal var height: Double = 0
static func ==(lhs: Equality, rhs: Equality) -> Bool {
return lhs.width == rhs.width && rhs.height == lhs.height
}
}
或者简单地说:
protocol Rectangle: Equatable {
var width: Double { get }
var height: Double { get }
}
extension Rectangle {
static func ==(lhs: Self, rhs: Self) -> Bool {
return lhs.width == rhs.width && rhs.height == lhs.height
}
}
class Equality: Rectangle {
internal var width: Double = 0
internal var height: Double = 0
}
这篇关于成员运算符“=="必须至少有一个类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!