Swift中的函数重新声明无效 [英] Invalid Redeclaration of function in Swift

查看:797
本文介绍了Swift中的函数重新声明无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一些帮助函数,并得到一些类似的方法的无效重复声明错误。如果有人能解释为什么这些方法碰撞我会很感激。

I'm trying to write some helper functions and am getting an 'Invalid Redeclaration' error of some similar methods. If anyone can explain why these methods collide I would be very thankful.

func CGRectModify(rect: CGRect, x: CGFloat) -> CGRect {
    return CGRectMake(x, rect.origin.y, rect.size.width, rect.size.height)
}

func CGRectModify(rect: CGRect, y: CGFloat) -> CGRect {
    return CGRectMake(rect.origin.x, y, rect.size.width, rect.size.height)
}

func CGRectModify(rect: CGRect, width: CGFloat) -> CGRect {
    return CGRectMake(rect.origin.x, rect.origin.y, width, rect.size.height)
}

func CGRectModify(rect: CGRect, height: CGFloat) -> CGRect {
    return CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, height)
}

我认为因为第二个参数有一个不同的外部名称,所以该方法应该被理解为具有不同的签名。似乎不是这样的。

I would think that because the second parameter has a different external name, the method would be understood to have a different signature. It seems that is not the case.

我一直在使用Apple的文档进行参考,但在方法的本地和外部参数名称我找不到我的答案。任何输入是非常感谢。

I've been using Apple's doc for reference, but in the section of Local and External Parameter Names for Methods I could not find my answer. Any input is much appreciated.

推荐答案

函数的自动外部参数名称的规则与方法不同。

The rule of "automatic external parameter names" for functions are different from methods.

Swift应用不同的规则取决于可调用的类型。

Swift applies different rules depends on the type of callables.


  1. strong>→文档

不执行自动外部参数名称。

No "automatic external parameter names" are performed.

func f(x:Int, y:Int) { /* ... */ }
let c =  { (x:Int, y:Int) -> Void in /* ... */ }

f(1, 2)
c(1, 2)


  • 初始化程序文档

    自动外部参数名称

    class Foo {
        init(x:Int, y:Int) { /* ... */ }
    }
    
    let foo = Foo(x: 1, y: 2)
    


  • 方法文档

    自动外部参数名称(除)作为第一个参数。

    "automatic external parameter names" except for the first parameter.

    extension Foo {
        func bar(x:Int, y:Int) { /* ... */ }
    }
    
    foo.bar(1, y:2)
    


  • 下标→文档缺失?

    不执行自动外部参数名称。

    No "automatic external parameter names" are performed.

    extension Foo {
        subscript(x:Int, y:Int) -> Void {
            get { /* ... */ }
        }
    }
    
    foo[1, 2]
    


  • ...的特殊规则...

    And a special rule for ...


    • 默认值

    func fz(x:Int, y:Int, z:Int = 1) { /* ... */ }
    
    fz(1, 1, z: 1)
    


    当然,您可以使用以下方法覆盖这些默认行为:

    Of course you can override these default behaviors using:


    • _ name:Type :禁用自动外部参数名称

    • #name:Type :forceautomatic external parameter names

    • externalName internalName:Type :显式外部名称

    • _ name:Type: disable "automatic external parameter names"
    • #name:Type: force "automatic external parameter names"
    • externalName internalName:Type: explicit external name

    这篇关于Swift中的函数重新声明无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆