I'm not quite understanding why the following code does not update the original values of array1
and array2
. I'm trying to use inout
on an array of iVars ...
import UIKit
class ViewController: UIViewController {
// MARK: instance variables
var array1: [Int] = [1, 1, 1]
var array2: [Int] = [2, 2, 2]
// MARK: lifecycle methods
override func viewDidLoad() {
super.viewDidLoad()
var tempArray = [array1, array2]
performOperationOn(&tempArray)
print("array1: \(array1)")
print("array2: \(array2)")
print("tempArray[0]: \(tempArray[0])")
print("tempArray[1]: \(tempArray[1])")
}
// MARK: private methods
private func performOperationOn(_ array: inout [[Int]]) {
array[0][0] = 11
array[0][1] = 11
array[0][2] = 11
array[1][0] = 22
array[1][1] = 22
array[1][2] = 22
}
}
...
The above prints out:
array1: [1, 1, 1]
array2: [2, 2, 2]
Is there any way to accomplish what I'm expecting:
array1: [11, 11, 11]
array2: [22, 22, 22]
Printing out tempArray does reveal it is being modified:
tempArray[0]: [11, 11, 11]
tempArray[1]: [22, 22, 22]
But I really want the OG arrays (array1 and array2) to be the ones being modified ... thoughts?