& can only be used to pass a variable as an inout argument to a function. So the easiest solution is perhaps to use a helper function
inside your function:
func foo() {
func helper(inout array : [Int]) {
array[2] = 99
}
var a = [1, 2, 3, 5, 6]
var b = [4, 5, 6, 7]
let someFlag = true
if someFlag {
helper(&a)
} else {
helper(&b)
}
// ...
}
You can create a reference to the array using UnsafeMutableBufferPointer:
let ref = someFlag ?
UnsafeMutableBufferPointer(start: &a, count: a.count) :
UnsafeMutableBufferPointer(start: &b, count: b.count)
ref[2] = 99
But there are two problems with this solution:
UnsafeMutableBufferPointer() creates a non-owning reference,
so the compiler might decide to deallocate the array while the reference
is still used.
- There is no bounds check on the array.
So to make this work safely, you have to add some code:
withExtendedLifetime(a) { () -> Void in
withExtendedLifetime(b) { () -> Void in
let ref = someFlag ?
UnsafeMutableBufferPointer(start: &a, count: a.count) :
UnsafeMutableBufferPointer(start: &b, count: b.count)
if ref.count > 2 {
ref[2] = 99
}
}
}
which is a bit ugly.