Swift Tuple index using a variable as the index?

前端 未结 5 848
灰色年华
灰色年华 2021-01-21 08:31

Swift Tuple index using a variable as the index? Anyone know if it is possible to use a variable as the index for a Swift tuple index. I wish to select and item from a tuple usi

5条回答
  •  盖世英雄少女心
    2021-01-21 09:26

    You can cast a tuple to a buffer only if it has an homogeneous type. But before doing so, wonder if an array can't do the job.

    var tuple: (Int, Int, Int, Int) = (0,1,2,3)
    
    withUnsafeMutablePointer(to: &tuple) { pointer in
        pointer.withMemoryRebound(to: Int.self, capacity: 4) { buffer in
            buffer[3] = 0
        }
    }
    
    print(tuple)
    

    result:

    (0, 1, 2, 0)
    

提交回复
热议问题