0

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]
                    
   }
}
3
  • 3
    This doesn’t make sense, your first array is an array of integers and nothing else and why do you need an array to update an object? Maybe we can help you if you explain what you are trying to do. Commented Mar 9, 2021 at 21:32
  • just change your array declaration to var myArray = [newTest.x, newTest.y, newTest.z] Commented Mar 9, 2021 at 21:51
  • @JoakimDanielson thanks for your help. I tried to explain it with the Updated question. Commented Mar 11, 2021 at 15:30

1 Answer 1

1

It seems a bit strange to take a list of comma separated values and use them to update an object but given that here is one way to do it.

Create a function to map between an index (in the array) and a property in your class

func update(at index: Int, with value: Int) {
    switch index {
    case 0:
        x = value
    case 1:
        y = value
    case 2:
        z = value
    default:
        print("Value \(value) at \(index) igonred")
    }
}

and use it like this

categoriesArray.compactMap(Int.init).enumerated().forEach(newTest.update)

Another solution is to work with KeyPath

func keyPath(for index: Int) -> WritableKeyPath<Test, Int>? {
    switch index {
    case 0:
        return \.x
    case 1:
        return \.y
    case 2:
        return \.z
    default:
        return nil
    }
}

and then use the following code to update the object using your array

categoriesArray.compactMap(Int.init).enumerated().forEach {
    if let keyPath = keyPath(for: $0.offset) {
        newTest[keyPath: keyPath] = $0.element
    }
}

I still think it would be better to have one input field in the UI for every property in your class so you can have a direct one-to-one connection between them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.