【问题标题】:Using Function Parameter Names in Swift在 Swift 中使用函数参数名称
【发布时间】:2014-07-10 15:40:03
【问题描述】:

在调用方法时使用 Swift 参数名称,但第一个参数除外。为什么不使用名字?

使用 Swift 手册的变体;

var count2: Int = 0
func incrementBy2(amount: Int, numberOfTimes times: Int) {
count2 += amount * times}

这会起作用的;

incrementBy2(2, numberOfTimes: 7)

但是,这给了我“调用中的无关参数标签‘金额’”

incrementBy2(amount: 2, numberOfTimes: 7)

这是否有原因,或者它是那些“本来就是这样”的事情之一?

【问题讨论】:

    标签: swift


    【解决方案1】:

    这是为了遵循我们都习惯于 Objective-C 的约定,其中第一个参数的名称与方法名称相结合。这是一个例子:

    - (void)incrementByAmount:(NSInteger)amount
                numberOfTimes:(NSInteger)times
    {
        // stuff
    }
    

    你可以这样调用方法:

    [self incrementByAmount:2 numberOfTimes:7];
    

    通过将参数名称合并到方法名称中,阅读起来感觉更自然。在 Swift 中,您可以通过以下方式实现相同的目的:

    func incrementByAmount(amount: Int, numberOfTimes: Int) {
        // same stuff in Swift
    }
    

    然后像这样调用方法:

    incrementByAmount(2, numberOfTimes: 7)
    

    如果您不想使用这种约定,Swift 让您能够更明确地定义单独的内部和外部参数名称,如下所示:

    func incrementByAmount(incrementBy amount: Int, numberOfTimes: Int) {
        // same stuff in Swift
        // access `amount` at this scope.
    }
    

    你可以这样调用方法:

    incrementByAmount(incrementBy: 2, numberOfTimes: 7)
    

    【讨论】:

    • 谢谢。这个概念现在很有意义。令人尴尬的是,虽然看起来很简单,但我没有抓住它。 :-)
    • 注意。现在在 Swift 2.0 中,您可以调用方法 func incrementByAmount(amount amount: Int, numberOfTimes: Int) { }。删除 # 符号并在参数名称上加倍:)
    【解决方案2】:

    在您的示例中,您没有为第一个参数设置名称。你只有变量名。

    请尝试以下操作:

    func incrementBy2(amount am: Int, numberOfTimes times: Int) {
    }
    

    所以你可以称它为

    incrementBy2(amount: 0, numberOfTimes: 0)

    am 将是函数内变量的名称。 amount 将是一个外部名称。

    如果你想要相同的外部名称和变量,你可以试试这个:

    func incrementBy2(amount amount: Int, numberOfTimes times: Int) {
    }
    

    仍然像这样打电话

    incrementBy2(amount: 0, numberOfTimes: 0)

    更多信息函数参数名称指定外部参数名称苹果The Swift Programming Language的章节

    【讨论】:

    • 谢谢!我只在这本书的第 21 页上,向前看一点对发生的事情更有意义。我现在明白了使用#来使用本地参数名称。
    • Swift 移除了 # 符号。现在你应该加倍(例如func incrementBy2(amount amount: Int...
    【解决方案3】:

    Swift 中的方法与它们在 目标-C。和 Objective-C 一样,Swift 中的方法名 通常使用介词来指代方法的第一个参数 如 with、for 或 by,如在 incrementBy 方法中所见 前面的 Counter 类示例。介词的使用使 方法在被调用时被读作句子。斯威夫特做到了这一点 通过使用易于编写的已建立的方法命名约定 方法参数的默认方法与其使用的不同 函数参数。

    具体来说,Swift 默认给方法中的第一个参数名称一个本地参数名称,并给第二个和后续 默认情况下,参数名称包括本地参数名称和外部参数名称。 此约定与您的典型命名和调用约定相匹配 将熟悉编写 Objective-C 方法,并为 无需限定参数即可表达的方法调用 名字。

    摘自:Apple Inc.“Swift 编程语言”。电子书。 https://itun.es/ru/jEUH0.l

    【讨论】:

    • 谢谢,这很有意义。我没有深入到书中去理解这一点。
    【解决方案4】:

    只是为了更新最新版本的 Swift 的答案,使用 # 强制在外部使用函数的第一个命名参数已从语言中删除。为了指定外部参数名称,您现在只需用空格将外部名称与内部名称隔开。 # 以前用于表示内部和外部同名的参数,但现在您必须在函数定义中将参数名称加倍才能实现:

    var count2: Int = 0
    func incrementBy2(amount amount: Int, numberOfTimes times: Int) {
      count2 += amount * times
    }
    incrementBy2(amount: 2, numberOfTimes: 7) // result is 14
    

    为了清楚起见,您还可以区分名称:

    var count2: Int = 0
    func incrementBy2(theValue amount: Int, numberOfTimes times: Int) {
      count2 += amount * times
      }
    incrementBy2(theValue: 2, numberOfTimes: 7)
    

    或者只是用Objective-C的方式,明确第一个参数应该是什么:

    var count2: Int = 0
    func incrementBy2TheValue(amount: Int, numberOfTimes times: Int) {
      count2 += amount * times
    }
    incrementBy2TheValue(2, numberOfTimes: 7)
    

    不过,我不确定人们对函数名称中的数字有何看法。但我意识到这只是一个例子。

    【讨论】:

    • 我喜欢# ...我想知道删除它的原因是什么
    【解决方案5】:

    它只是使用与目标 C 中相同的模式,其中方法具有命名参数,除了第一个参数,它由方法名称本身定义。例如

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    您可以通过显式添加名称或在参数名称前加上井号 # 字符来将第一个参数转换为命名参数。

    顺便说一句,在Objective C中,方法签名是方法名加上所有命名参数,例如:

    - (void) addUser:(User *)user toArray:(NSMutableArray *)array
    

    不同于:

    - (void) addUser:(User *)user toDict:(NSMutableDictionary *)dict
    

    几乎与其他语言(C#、Java 等)一样:

    void addUser(User user, Array array)
    

    不同于:

    void addUser(User user, Dictionary dict)
    

    【讨论】:

    • 谢谢。我没有深入到书中去抓住它。
    猜你喜欢
    • 2017-06-11
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2017-09-19
    • 1970-01-01
    • 2015-11-09
    相关资源
    最近更新 更多