我想用**来重载指数函数.如果我使用类似“^”的东西,我会工作,但
python的做法是**,我想用
Swift.有办法吗?
error: Operator implementation without matching operator declaration
@infix func ** (num: Double,power: Double) -> Double{ return pow(num,power) } println(8.0**3.0) // Does not work
您需要在定义函数之前声明运算符,如下所示:
原文链接:https://www.f2er.com/swift/319416.html在Swift 2中:
import Darwin infix operator ** {} func ** (num: Double,power: Double) -> Double { return pow(num,power) } println(8.0 ** 3.0) // works
在Swift 3中:
import Darwin infix operator ** func ** (num: Double,power) } print(8.0 ** 3.0) // works