> ## Content Index
> Fetch the complete content index at: https://yinguobing.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# CS193P: Lecture 2中的一个函数重载编译错误
- URL: https://yinguobing.com/blog/cs193p-lecture-2-compile-error/
- Published: 2015-05-24T17:11:49.000Z
- Updated: 2017-10-26T03:30:43.000Z
- Description: 在第二节课的作业中遇到一个问题。Paul在计算器的例子中展示了函数重载的用法，在编译的时候报错了。
- Author: 尹国冰
- Tags: Swift

在完成[CS106A](https://yinguobing.com/blog/stanford%e5%85%ac%e5%bc%80%e8%af%be%ef%bc%9aprograming-methodology-cs106a/)与[CS106B](https://yinguobing.com/blog/stanford-open-programming-abstractions-cs106b/)之后，终于又回到了[CS193P](https://yinguobing.com/blog/%e6%96%af%e5%9d%a6%e7%a6%8f%e5%a4%a7%e5%ad%a6%e7%9a%84swift%e8%af%ad%e8%a8%80%e8%af%be%e7%a8%8b/)的课程之中。

在第二节课的作业中遇到一个问题。Paul在计算器的例子中展示了函数重载的用法，具体是`performOperation`函数存在两种用法，一种接受2个参数，另一种接受1个参数：

```
func performOperation(operation: (Double, Double) -> Double)<br></br>
func performOperation(operation: Double -> Double

```

但是在编译的时候编译器报错了：

```
Method ‘performOperation’ with Objective-C selector ‘performOperation:’ conflicts with previous declaration with the same Objective-C selector

```

检索了一下，大致原因是[“Obj-C不支持方法重载”](http://www.cocoachina.com/bbs/read.php?tid=297461&ref=yinguobing.com)（参看来源2楼）。而教程中Paul使用的Xcode与我现在使用的不同，我用的6.3.2版本更新了Swift 1.2版，[对于type-based overloading进行了更加严格的检查](http://stackoverflow.com/questions/29457720/compiler-error-method-with-objective-c-selector-conflicts-with-previous-declara/29670644?ref=yinguobing.com#29670644)。因此这段代码编译出错。

[解决方案](http://tieba.baidu.com/p/3717991985?ref=yinguobing.com)也有，利用`@objc`修饰符，它起的作用是[为Objective-C重新声明方法](http://mobile.51cto.com/hot-465065.htm?ref=yinguobing.com)名称：

```
@objc(operationOne:) func performOperation(operation: (Double, Double) -> Double)
@objc(operationTwo:) func performOperation(operation: Double -> Double

```

只要`@objc`后边的参数名称不同即可。