swift编译错误

最近在跟着斯坦福大学的ISO8开发学习
下面的代码,编译出错

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

    }

}

出现下面的错误:

Method 'performOperation' with Objective-C selector 'performOperation:' conflicts with previous declaration with the same Objective-C selector

湖上湖
浏览 813回答 2
2回答

幕布斯7119047

如果你这个类没继承Objective-C的类,代码不会出错的,显然你这个类继承了一个Objective-C的类,Objective-C不支持方法重载,而这两个方法的selector是一样的,所以就错了。要么你给两个方法起不一样的名字,要么你想办法让这个类不要继承Objective-C的类,要不你把其中一个设成private,或者你还可以把其中的一个方法加一个Objective-C不支持的参数类型比如()=()之类的。总之就是不要让这两个方法同时和Objective-C打交道就没问题。话说新版的Swift应该能编译过去,但运行的时候会挂。
打开App,查看更多内容
随时随地看视频慕课网APP