Destructuring tuple of tuple in closure

柔情痞子 提交于 2019-12-10 13:28:00

问题


I can destructure a tuple of tuple easily:

let tt = (2, (3, 4))

let (a, (b, c)) = tt
b // => 3

I'd like to do the same when declaring a closure, for example I thought I could write:

[tt].map { (a, (b, c)) in
    // Use b
}

Xcode complains with "Unnamed parameters must be written with the empty name".

Only way I got it to "work" was:

[tt].map { (a, tuple: (b: Int, c: Int)) in
    // Use tuple.b
}

This has two drawbacks I'd like to avoid:

  • I need to use tuple.b instead of b
  • I need to specify the types of b and c

BTW, my use case is that I want to do a reduce with index so I'm trying using array.enumerate().reduce


回答1:


With an additional assignment line, you can assign the values in the array to (a, (b, c)) to deconstruct the tuple:

let tt1 = (2, (3, 4))
let tt2 = (5, (6, 7))

[tt1, tt2].map { tt in
    let (a, (b, c)) = tt
    print(b)
}

Output:

3
6

Alternatively:

[tt1, tt2].map {
    let (a, (b, c)) = $0
    print(b)
}



回答2:


This satisfies your first requirement but still require you to add type annonation:

typealias TupleType = (a: Int, tuple: (b: Int, c: Int))

let tt: TupleType = (2, (3, 4))

[tt].map {
    print($0.tuple.b)
}



回答3:


Another workaround:

[tt].map { a, b in
    let (b, c) = b
    print(a, b, c)
}


来源:https://stackoverflow.com/questions/37358171/destructuring-tuple-of-tuple-in-closure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!