简体   繁体   中英

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

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

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.

有人问了一会儿……但以防万一我认为你可能正在寻找https://cocoapods.org/pods/EVReflection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM