Swift函数接受元组

Tam*_*isk 8 swift

只要它们的类型匹配,是否可以将元组传递给函数?

当我尝试它时,我收到一个missing argument in parameter错误:

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

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

myFunction(myTuple)
Run Code Online (Sandbox Code Playgroud)

Dre*_*rew 7

虽然在Swift 2.2中被弃用,但它有可能:

在Swift 2.1和更早版本中,可以使用精心设计的元组来填充函数的参数.因此,如果你有一个带有两个参数的函数,只要元组具有正确的类型和元素名称,就可以使用两元素元组调用它.

...

这种语法 - 被亲切地称为"元组splat语法" - 是惯用的Swift的自我记录,可读样式的对立面,所以它在Swift 2.2中被弃用了.

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


Obl*_*ely 6

我来到这里想要知道如何将元组作为函数参数传递.这里的答案集中在一个不同的案例上.我不太清楚OP之后是什么.

在任何情况下,这里是如何将元组作为参数传递.并且,为了更好的衡量,如何可变地进行.

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'
Run Code Online (Sandbox Code Playgroud)

传递元组可以是一种快速方便的方法,使您不必创建包装等.例如,我有一个用例,我传递一组标记和参数来创建游戏级别.元组使这个美观和紧凑:

// 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"))
Run Code Online (Sandbox Code Playgroud)


Ant*_*nio 5

是的,在这些条件下可能:

  • 元组必须是不可变的
  • 元组中的值的数量,它们的类型和顺序必须与函数预期的参数匹配
  • 命名参数必须与函数签名中的外部名称匹配
  • 非命名参数必须与函数签名中没有外部名称的参数匹配

所以,你的代码是好的,你唯一要做的就是把元组变成一个不可变的元组(即使用let和不使用var):

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

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

myFunction(myTuple)
Run Code Online (Sandbox Code Playgroud)

另外一个带有外部名称的例子:

let myTuple = ("Text", paramB: 10, paramC: "More Text")

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

myFunction(myTuple)
Run Code Online (Sandbox Code Playgroud)


Mic*_*sky 3

在您的元组中,似乎您必须命名它们,然后这样引用它们:

所以你的代码应该是

var myTuple = (val1: "Text", val2: 10, val3: "More Text")

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


    myFunction(myTuple.val1, myTuple.val2, myTuple.val3)
Run Code Online (Sandbox Code Playgroud)

元组具有命名值(val1、val2、val3),您可以设置这些值,然后在传入 myTuple 时引用函数 myFunction(),看起来好像您只是填充了 3 个可用参数中的 1 个 - 并且使用启动类型错误!这相当于将类型存储在元组中,然后取出它们进行函数调用。但是,如果您希望函数实际上采用元组作为参数,请参见下文:

var myTuple = (val1: "Text", val2: 10, val3: "More Text")

    func tupleFunc(a:(String, Int, String)) {

    }

    tupleFunc(myTuple)
Run Code Online (Sandbox Code Playgroud)