简体   繁体   English

Swift中的函数参数类型

[英]Function parameter type in Swift

In Swift Programming Language Guide, under Function Type section, it says “Because the parameter type and the return type can be a tuple type, function types support functions and methods that take multiple parameters and return multiple values.” 在《 Swift编程语言指南》的“函数类型”部分下,它表示“由于参数类型和返回类型可以是元组类型,因此函数类型支持采用多个参数并返回多个值的函数和方法。”

Note the use of word "can be" - does it mean parameter type can be something else as well? 注意使用“可以”一词-这是否意味着参数类型也可以是其他类型? Or parameter type has to be tuple only? 还是参数类型必须仅是元组?

you can either call a function with tuples OR with arguments 您可以使用元组或带有参数的函数

eg 例如

func sum(a: Int, b: Int) -> Int {
  return a + b
}

you could call this: 您可以这样称呼:

let numbers = (40,2)
sum(numbers)

or the old way like 或像

sum(40,2)

A tuple means a set of parameters instead of "single" parameters. 元组表示一组参数,而不是“单个”参数。

The default way means one value per parameter like this: 默认方式表示每个参数一个值,如下所示:

func setValue( myValue:Int ) { ... }
setValue( 3 )

On the other hand a tuple can be more than one parameter: 另一方面,元组可以是多个参数:

func setValues( myValues:(Int,String) ) { ... }
setValues( (1, "Hello") )

So tuples are possible, but the common way is to use only single values 因此,元组是可能的,但是常见的方法是仅使用单个值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM