我想知道Swift中是否有方法或函数可以计算元组中元素的数量.
问题原因:
var tople = (1,2,3)
func funkce (a:Int, b:Int, c: Int){
println(a)
println(b)
println(c)
}
funkce(a,b,c)
Run Code Online (Sandbox Code Playgroud)
谢谢,我非常感谢任何有用的评论.
在Swift 3中,你可以计算元组中元素的数量,如下所示:
let myTuple = (1,2,3,4)
let size = Mirror(reflecting: myTuple).children.count
Run Code Online (Sandbox Code Playgroud)
是的,您可以将元组加载到变量中.
let (a, b, c) = (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
要从元组中提取值,可以使用MirrorType.你可以在这里找到更多.
let tuple = (1, 2, 3)
let count = reflect(tuple).count // 3
Run Code Online (Sandbox Code Playgroud)