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

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

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()
    }
}

图片描述

阅读 9.5k
5 个回答

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

新手上路,请多包涵

没有关键字。override。

新手上路,请多包涵
新手上路,请多包涵

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() {...}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进