Swift: How to better format the output when using println with a tuple? -
i have defined function:
func need_rebalance() -> (bool, rebalancestrategy) { }
where rebalancestrategy enum type
enum rebalancestrategy: string { case leftright = "leftright" case rightleft = "rightleft" }
when tried print way,
println("need rebalance? \(self.need_rebalance())")
i got output this:
need rebalance? (false, (enum value))
my questions are:
1) there easy extract value tuple? (hopefully similar python e.g. self.need_rebalance()[1]. apparently syntax not work in swift because tuple not support subscript()
)
2) how can print raw value of enum instead of having (enum value)
?
i using xcode6 beta5
there's way extract value using tuple indexes, it's not nice, involves reflect
:
let tuple = self.need_rebalance() let reflection = reflect(tuple) reflection[0].1.value // -> true reflection[1].1.value // -> rebalancestrategy.?
also, if tuple members not named:
let tuple = self.need_rebalance() tuple.0 // -> true tuple.1 // -> rebalancestrategy.?
to access raw value in enum
:
rebalancestrategy.leftright.toraw()
Comments
Post a Comment