Swift函数参数标签术语的更改 [英] Changes in terminology for Swift function parameter labels

查看:134
本文介绍了Swift函数参数标签术语的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift提供了为函数参数提供内部和外部名称/标签的功能。但最近,苹果公司似乎诉诸于只说参数和参数的名称/标签,并放弃使用内部/外部来描述这些事情。

在Swift文档和WWDC视频中,描述函数参数和参数之间的区别有一些不明确的努力,而没有将这些区分为外向或internal,例如


每个函数参数都有参数标签和参数
名称。参数标签在调用函数时使用;每个
参数都在函数调用中写入,其参数标签为
。参数名称用于执行
函数。默认情况下,参数使用其参数名称作为它们的
参数标签。 )函数。



在内部,when和height的名称可能不同,它们是内部名称。除了函数定义和声明中的顺序之外,外部和内部似乎并不令人困惑:

  func跳转(_谁:字符串,whenToJump时:浮动,如何高高度:INT){
//等待whenToJump
//调整who.y由howHigh

这是一个参数,苹果指的是这个参数,哪一个是参数?

解决方案


函数参数标签和参数名称

b
$ b

位于 Swift编程语言(Swift 3)



简单地说, Swift 2 Swift 3


  • 外部名称(Swift 2 )现在是函数参数标签(Swift 3)
  • 现在Parameter Name(Swift 3)
  • (Swift 2)中,第一个参数是默认情况下,(内部但不是外部)
  • (Swift 3)中,第一个参数是名称名称(函数参数标签和参数名称)。


Swift provides the ability to give both an internal and external name/label for parameters of functions. But lately Apple seems to have resorted to only saying "Argument" and "Parameter" names/labels and dropped the use of internal/external to describe these things.

In the Swift documents, and WWDC videos, there are a few unclear efforts to describe the difference between a function's parameters and arguments, without referring to these as the outward facing or internal, such as:

Each function parameter has both an argument label and a parameter name. The argument label is used when calling the function; each argument is written in the function call with its argument label before it. The parameter name is used in the implementation of the function. By default, parameters use their parameter name as their argument label.

Imagine a jump() function.

Internally, the names of "when" and "height" might be different, and these are the internal names. External and internal don't seem to be at all confusing, other than the ordering in the Function Definition and then Declaration:

 func jump(_ who: String, whenToJump when: Float, howHigh height: Int){
        // wait for whenToJump
       // adjust who.y by howHigh
    )

Which of these is a parameter, in the sense Apple is referring to them, and which is an argument?

解决方案

Everything is described in detail in the section Function Argument Labels and Parameter Names

in The Swift Programming Language (Swift 3)

Briefly, the differences between Swift 2 and Swift 3 are

  • "External name" (Swift 2) is now "Function Argument Label" (Swift 3)
  • "Internal name" (Swift 2) is now "Parameter Name" (Swift 3)
  • In (Swift 2) the first parameter is _ name (internal, but no external) by default
  • In (Swift 3) the first parameter is name name (function argument label and parameter name) by default.

这篇关于Swift函数参数标签术语的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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