swift重载函数难道不是这么写的吗?

发布于 2022-09-01 12:00:58 字数 567 浏览 19 评论 0

请各位大神自动忽略无关代码,关注函数定义,附图一张

func performOperation(operation: (Double,Double) -> Double)
{
    if operandStack.count >= 2
    {
        displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
        enter()
    }
}
 func performOperation(operation: (Double) -> Double)
{
    if operandStack.count >= 1
    {
        displayValue = operation(operandStack.removeLast())
        enter()
    }
}

图片描述

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不离久伴 2022-09-08 12:00:58

因为你看到的视频,那个老头子用的是旧版xcode,新版xcode会检查了继承过来的object-c对象是否有重载了。老老头子用的是swift的重载特性,但object-c重载是不支持重载的。版本问题。

我们只是彼此的过ke 2022-09-08 12:00:58

没有关键字。override。

草莓酥 2022-09-08 12:00:58

http://www.cocoachina.com/bbs/read.php?tid=297461#1290696
这里讲的很清楚,继承的类不同的原因(UIViewController)

从﹋此江山别 2022-09-08 12:00:58

Objective-C does not support method overloading, you have to use a different method name. When you inherited UIViewController you inherited NSObject and made the class interopable to Obj-C. Swift on the other hand does support overloading, that's why it works when you remove the inheritance.

As it has already been answered, ObjC doesn't support method overloading (two methods with the same name) and In swift 2 under Xcode 7 there are two options to solve this kind of problems. One option is to rename the method using the attribute:

@objc(newNameMethod:)

func methodOne(par1, par2) {...}

@objc(methodTwo:)
func methodOne(par1) {...}

another option to solve this problem in Xcode 7+ is by applying @nonobjc attribute to any method, subscript or initialiser

func methodOne() {...}

@nonobjc
func methodOne() {...}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文