为什么我会收到此错误(swift 2.2,处理 func 和命名参数)?

Why am i getting this error (swift 2.2, dealing with func and named parameters)?

这段代码写在Xcode 7.3。我不知道为什么我会收到此错误。在 swift 1.0 中它运行良好。但在 swift 2.2 中不是。

下面应该按预期工作:

func isDivisible(divided: Int, divisor: Int) -> Bool {
    if divided % divisor == 0 {
        return true
    }
    else {
        return false
    }
}

来自 Swift 2.x 文档:

Local and External Parameter Names for Methods

Function parameters can have both a local name (for use within the function’s body) and an external name (for use when calling the function), as described in Specifying External Parameter Names. The same is true for method parameters, because methods are just functions that are associated with a type.

...

Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default. This convention matches the typical naming and calling convention you will be familiar with from writing Objective-C methods, and makes for expressive method calls without the need to qualify your parameter names.


要完全匹配屏幕截图中的 Swift 1.0 语法,您必须编写

func isDivisible(divided divided: Int, divisor: Int) -> Bool {}

顺便说一句:if divided % divisor不编译,你可以用

替换整个函数体
return divided % divisor == 0