Any way to iterate a tuple in swift?

后端 未结 6 664
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 15:24

I am curious how to do a for loop with a tuple in swift.

I know that to access each member you can use dot notation using the index number

var tuple         


        
6条回答
  •  青春惊慌失措
    2020-12-04 16:04

    You can using reflection Swift 5

    Try this in a Playground:

    let tuple = (1, 2, "3")
    let tupleMirror = Mirror(reflecting: tuple)
    let tupleElements = tupleMirror.children.map({ $0.value })
    tupleElements
    

    Output:

提交回复
热议问题