, . , , , . , , , , .
struct TupleWrapper {
let tuple: (String, Int, String, String, String, String, String, Bool)
let count: Int
private let mirrors: [MirrorType]
subscript (index: Int) -> Any {
return mirrors[index].value
}
init(tuple: (String, Int, String, String, String, String, String, Bool)) {
self.tuple = tuple
var mirrors = [MirrorType]()
let reflected = reflect(tuple)
for i in 0..<reflected.count {
mirrors.append(reflected[i].1)
}
self.mirrors = mirrors
self.count = mirrors.count
}
}
let wrapper = TupleWrapper(tuple: ("1", 2, "3", "4", "5", "6", "7", false))
wrapper[0]
wrapper[wrapper.count - 1]
The above code uses the Swift display APIs to get the logical children of your tuple and add their mirrors to an array on which we can index for each value of the mirror. This is pretty straight forward, but, as you would expect, since we work with tuples, it can in no way be dynamic. A wrapper must be made for a specific type of tuple.
For some related readings, see a recent NSHipster MirrorType article .
source
share