I was wondering if there is a possibility to set a class property via an array:
class Test {
var x: Int = 0
var y: Int = 0
var z: Int = 0
}
let newTest = Test()
let myArray = [newTest.x, newTest.y, newTest.z]
So I created an Array with the properties of a class. Now I want to access the property object itself and set it to a value, kind of like this (I know this sets the array at index 0 to 1, but I hope the general idea is clear):
myArray[0] = 1 // I want to set newTest.x = 1
The following one of course works but I need the property objects themself in the array:
let myArray = [newTest, newTest, newTest]
myArray[0].x = 1
UPDATE
What I have now is an array with integers, but I need the reference to the class property, so I can set its value.
The Use Case: I have an TextField as a user input. The user input should be splitted by a ','. And I don't know in advance, how many items I will get. And therefore I thought I run a for loop, so I can set class properties based on the number of items.
newItem = Item()
var myClassPorperties = [newItem.cat1, newItem.cat2, newItem.cat3]
if let categoriesArray = categoriesTextField.text?.components(separatedBy: ",") {
for i in 0...categoriesArray.count - 1 {
myClassProperties[i] = categoriesArray[i]
}
}
var myArray = [newTest.x, newTest.y, newTest.z]