I came here wanting to learn how to pass a tuple as a parameter to a function. The answers here focus on another case. I do not quite understand what OP is.
In any case, here's how to pass a tuple as a parameter. And, for a good measure, how to do it in variations.
func acceptTuple(tuple : (Int, String)) { print("The Int is: \(tuple.0)") print("The String is '\(tuple.1)'") } acceptTuple((45, "zebras")) // Outputs: // The Int is: 45 // The String is 'zebras' func acceptTuples(tuples : (Int, String) ...) { var index = 0 // note: you can't use the (index, tuple) pattern in the for loop, // the compiler thinks you're trying to unpack the tuple, hence /// use of a manual index for tuple in tuples { print("[\(index)] - Int is: \(tuple.0)") print("[\(index)] - String is '\(tuple.1)'") index++ } } acceptTuples((45, "zebras"), (17, "armadillos"), (12, "caterpillars")) //Outputs //[0] - Int is: 45 //[0] - String is 'zebras' //[1] - Int is: 17 //[1] - String is 'armadillos' //[2] - Int is: 12 //[2] - String is 'caterpillars'
Passing tuples in can be a quick and convenient approach, which eliminates the need to create wrappers, etc. For example, I have a use case when I pass a set of tokens and parameters to create a game level. The tuple makes it nice and compact:
// function signature class func makeLevel(target: String, tokens: (TokenType, String)...) -> GameLevel // The function is in the class Level. TokenType here is an Enum. // example use: let level = Level("Zoo Station", tokens: (.Label, "Zebra"), (.Bat, "LeftShape"), (.RayTube, "HighPowered"), (.Bat, "RightShape"), (.GravityWell, "4"), (.Accelerator, "Alpha"))