You cannot return the value inout . Because the compiler cannot guarantee the lifetime of the value.
You have an unsafe way, for example:
var a = [1, 2] var b = [3, 4] func arrayToPick(i:Int) -> UnsafeMutablePointer<[Int]> { if i == 0 { return withUnsafeMutablePointer(&a, { $0 }) } else { return withUnsafeMutablePointer(&b, { $0 }) } } var d = arrayToPick(0) d.memory[0] = 6 println(a[0])
In this case, after the release of a d.memory access may result in a BAD_ACCESS error.
Or safe , for example:
var a = [1, 2] var b = [3, 4] func withPickedArray(i:Int, f:(inout [Int]) -> Void) { i == 0 ? f(&a) : f(&b) } withPickedArray(0) { (inout picked:[Int]) in picked[0] = 6 } println(a[0])
In this case, you can access the selected value only in closing.
rintaro
source share