3

I have this playground example:

import UIKit

var testArray: [[String]] = [["2","2"],["1","1"]]

testArray[0][0] = "3" // Working

func getTestArray() -> [String] {

    return testArray[0]

}

var test = getTestArray()[0] = "4" // Error: Immutable Value

How can i get the reference of testArray to change it

Changing test won't change testArray!

Jonas
  • 2,139
  • 17
  • 38

3 Answers3

3

getTestArray() returns a value, not a reference. Therefore, the value returned by a function cannot be edited, only variables can be changed. To change the variable, use:

var testVal = getTestArray() // get the value
testVal[0] = "4" // change the value
testArray[0] = testVal // SET the value of the reference `testArray` (change the value)
Papershine
  • 4,995
  • 2
  • 24
  • 48
0

You need to assign value to test and than change it:

var test = getTestArray()
test[0] = "4"
Ilya Kharabet
  • 4,203
  • 3
  • 15
  • 29
0
var test = getTestArray()
test[0] = "4"
print(test)
Catherine
  • 654
  • 5
  • 15