let tuple = ("banana", "sock", "shoe")
let first = { (t: (String, String, String)) -> String in t.0 }
let second = { (t: (String, String, String)) -> String in t.1 }
let third = { (t: (String, String, String)) -> String in t.2 }
let choice = first
print(first(tuple))
2条答案
按热度按时间6qqygrtg1#
元组不够灵活,无法做到这一点。您可以使用函数来接近:
但这根本无法扩展;您需要一组这样的函数来与 * 每个 * 元组类型进行交互。
一种选择是创建一个
struct
作为元组的替代,然后就可以使用KeyPath
。例如:或者,如果元组都是同构的(就像示例中的一样),另一种选择是转换为数组并使用数组下标:
(类似于下标的键路径也会出现在未来的Swift中,但在这种情况下,它们不会给你带来任何超出常规下标的东西。)
plicqrtu2#