1

Suppose I have a person class and each person has 100 variables, called v1 thru v100. Let's say my instance of person is called p. I get an array with 100 values and I want to assign the first value to p.v1, the second to p.v2, etc.

How can you do this in Swift 3?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • 5
    `each person has 100 variables, called v1 thru v100` This is not good practice. You should try using an array instead. You will be able to filter, populate, manage the arrays and their contents easily and efficiently. // Show the *relevant* parts of your existing code if you want more specific help. – Eric Aya Jan 09 '17 at 17:49
  • 1
    why can't you just use the indexes since that is how you are treating the variables? – epascarello Jan 09 '17 at 17:59
  • Take a look to this question about reflection in Swift: http://stackoverflow.com/questions/25463146/iterate-over-object-class-attributes-in-swift – nbloqs Jan 09 '17 at 17:59
  • 1
    @nbloqs Please don't show that. He should fix his model, he should not try to find workarounds. – Sulthan Jan 09 '17 at 18:03
  • I think that he just gave an example with those "Vx" names. I agree with you that the model is wrong, but his answer is how to do something and that question may have help with that specific question. – nbloqs Jan 09 '17 at 18:05
  • I'm not actually using a model like this--I was simply curious (very curious) to know if you could do something like this in Swift. I thought of it because I didn't know how to create an array of images in core data and thought that as a possibility I could just have N separate picture variables---and this led to me wondering how to efficiently assign these variables from an array of images. Anyway, I appreciate Eric's response and I also appreciate nbloqs pointing me in the direction of reflection. Thanks! – SwiftNoob Jan 20 '17 at 13:44

0 Answers0