Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift functions accepting tuples

Tags:

swift

Is it possible to pass in a tuple into a function as long as their types match up?

When I try it, I get a missing argument in parameter error:

var myTuple = ("Text",10,"More Text")

func myFunction(a:String, b:Int, c:String) {
    // etc...
}

myFunction(myTuple)
like image 256
Tamarisk Avatar asked Oct 13 '14 01:10

Tamarisk


People also ask

Can we write function inside a tuple in Swift?

In Swift 2.1 and earlier it was possible to use a carefully crafted tuple to fill the parameters of a function. So, if you had a function that took two parameters, you could call it with a two-element tuple as long as the tuple had the correct types and element names.

Can Swift func return multiple values?

Tuples can be used to return multiple values from Swift functions. A tuple allows you to group together multiple values of different types which can then be returned from a function as a single entity.

Can you pass a function as a parameter in Swift?

Every function in Swift has a type, consisting of the function's parameter types and return type. You can use this type like any other type in Swift, which makes it easy to pass functions as parameters to other functions, and to return functions from functions.

What is Variadic function in Swift?

In computer programming, a variadic function is a function which accepts a variable number of arguments. The function arguments are represented by … (three period characters) after the argument's type that can be accessed into their body as an array .


1 Answers

It was possible, although was deprecated in Swift 2.2:

In Swift 2.1 and earlier it was possible to use a carefully crafted tuple to fill the parameters of a function. So, if you had a function that took two parameters, you could call it with a two-element tuple as long as the tuple had the correct types and element names.

...

This syntax — affectionately called “tuple splat syntax” — is the antithesis of idiomatic Swift’s self-documenting, readable style, and so it’s deprecated in Swift 2.2.

https://swift.org/blog/swift-2-2-new-features/

like image 93
Drew Avatar answered Sep 28 '22 10:09

Drew