2

I am using struct in swift.

class Constants {
    struct const {
        static let signupFirstName = "signupFirstName"
    }
} 

I want to iterate the struct. For iterating I am using :

let mirrored_object = Mirror(reflecting: Constants.const())
for (index, attr) in mirrored_object.children.enumerate() {
        if let property_name = attr.label as String! {
            print("Attr \(index): \(property_name) = \(attr.value)")
        }
} 

But it does not enter into the code because of static value. Is there any way to iterate this struct?

2 Answers 2

2

Since static members are technically part of the type and not the instance, you would need to approach it this way to reflect on the type itself:

let mirrored_object = Mirror(reflecting: Constants.const.self)

However, Swift's automatic reflection for types themselves doesn't appear to be implemented at this time, so even the above line won't work.

That leaves one last option, which is defining your own custom mirror for reflecting on an instance of your type. That could look something like this:

class Constants {
    struct const : CustomReflectable {
        static let signupFirstName = "signupFirstName"
        func customMirror() -> Mirror {
            return Mirror(self, children: ["signupFirstName" : const.signupFirstName])
        }
    }
}

If you modify your code with a CustomReflectable implementation similar to the above, your loop to iterate through the struct members will now work.

Swift reflection will eventually get better out of the box, you might want to try a different approach until then.

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

2 Comments

Thank you for your answer. Suppose I have multiple number of Static variables in the Struct like : static let signupLastName = "signupLastName",static let signupEmail = "signupEmail" etc. Now instead of "return Mirror(self, children: ["signupFirstName" : const.signupFirstName])" , I want to dynamically loop this struct. How can I do? Please help.
@user3897036 Unfortunately, there is no way to dynamically loop through anything to create the custom mirror. The initializer for MirrorType only accepts a dictionary literal, which ultimately means that you have to pass in the exact values you want reflected, and there is no possibility for looping or generating based on variables, etc. :( Again, Swift reflection will get better down the road, but right now it's extremely limited and not very useful. You might want to try a different approach for what you are setting out to accomplish.
0

You can do it directly by accessing the variable at class level

if let property_value = const.signupFirstName as String! {
   print("hello \(property_value)")
}

Be sure to access it from the class it self const not an instance const(). Because the variable is static you can not access it from instance, you have to directly use the class

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.