4

I declared a struct with 4 properties ( informationA, informationB, informationC, informationD ).

Also I've declared an array like this (The array includes some names of the my_struct properties as "strings" :

let keys = ["informationA", "informationB", "informationC"]

Now I'd like a for-loop to go through the "keys"-array and print out the struct property values for the current string ("informationA", "informationB", "informationC").


struct my_struct {
    var informationA = "test"
    var informationB = "test...test"
    var informationC = "test...test"
    var informationD = "test...test..."
}



func getInformation() {
   let keys = ["informationA", "informationB", "informationC"]
   for i in keys {
       print(my_struct.i) // ERROR: Type 'my_struct' has no member 'i'

      // should print ---> "test", "test...test", "test...test"
   }
}

Using the code from above I'm getting this error ERROR: Type 'my_struct' has no member 'i'. Is there a way to avoid this message and achieve the result I'd like to get?

3
  • There are a number of answers that suggest using reflection, but I think it's worth asking why you want to do this. If its for debugging purposes, fine, use reflection. But if it's for the production code, there might be better patterns. It might be useful to understand the broader problem that you're trying to solve with this attempt to access properties of struct with string representations of the property name. Commented Jan 27, 2018 at 17:51
  • @Rob might I ask why using reflection in production might not be the best solution? For myself I have a struct of People each with their own Skills and i'd like to filter them based on what skills they have based on a boolean. Commented Jun 22, 2021 at 20:11
  • 1
    @Robin - IMHO, reflection is more of a debugging tool (e.g., Debugging and Reflection), for examining the properties of some random object. Its somewhat antithetical to a strongly-typed language. And I've never benchmarked it, but I wager it is not engineered for runtime efficiency, either. In your example, I would reach for Set<Skill>, or something like that, something that better captures the salient information in my actual model rather than relying on what is, effectively, metadata. Commented Jun 22, 2021 at 20:34

1 Answer 1

9

What you are looking for is reflection:

struct MyStruct {
    var informationA = "test"
    var informationB = "test...test"
    var informationC = "test...test"
    var informationD = "test...test..."
}

func getInformation() {
    let my_struct = MyStruct()
    let keys = ["informationA", "informationB", "informationC"]
    let m = Mirror(reflecting: my_struct)
    let properties = Array(m.children)

    for k in keys {
        if let prop = properties.first(where: { $0.label == k }) {
            print(prop.value)
        }
    }
}
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.