所以我突然收到了swift 3或(我认为(swift 4.0上没有的编译器警告。下面的代码使+=运算符过载以执行矢量增量:
public func += ( left: inout CGVector, right: CGVector) {
left += right
}
并发出警告,我很困惑,有人能解释为什么发出警告以及出了什么问题吗?
执行left += right
时,它调用与您定义的函数相同的函数。换句话说,您的运算符重载函数+= ( left: inout CGVector, right: CGVector)
将始终调用自己(无限递归(。你正在做类似的事情
func foo(String: bar) {
foo(bar)
}
但是仅仅用+=
代替foo
是不合乎逻辑的。Xcode现在只给你一个警告,但它并不是一个阻止你编译的错误。您可能在过去写错了这个函数(但提醒您这一点的警告只是添加到编译器中(。
你可能想要这样的
public func += ( left: inout CGVector, right: CGVector) {
left = CGVector(dx: left.dx + right.dx, dy: left.dy + right.dy)
}