How can convert Mirror To Orginal class or struct using swift language

Viewed 492

i have an question about the Mirror reflection . i convert my struct to mirror to iterate through all the properties to get values and after i iterate through it and change the values in properties i need to convert mirror again to the original struct with values which i edited but i can't , is swift language have way to do this conversion ?

the code below

//MARK:- loop get tags
    func getTags(filter: Any){

        let getTags = Mirror(reflecting: filter)
        for (tag) in getTags.children {

            if let getTag = tag.value as? String {
                if let _ = Int(getTag) {

                }else {
                    if getTag != "" && getTag != "All" {
                        arrayOfTags.append(getTag)
                    }
                }
            }// if let
        }// end for loop

    }

thanks

2 Answers

You can't construct a struct without hardcore memory manipulation. You could create objets with functions that are still available from Objective C. You could set the property with the setValue forKey function. Your objects needs to be derived from NSObject.

Doing this and taking into account all scenario's is quite a challenge. There is a CocoaPod library that could help with this. Have a look at EVReflection You could create a dictionary from your object and an object from your dictionary.

Related