简体   繁体   English

如何使用swift语言将镜像转换为原始类或结构

[英]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.您可以使用Objective C 中仍然可用的函数创建对象。您可以使用setValue forKey 函数设置属性。 Your objects needs to be derived from NSObject.您的对象需要从 NSObject 派生。

Doing this and taking into account all scenario's is quite a challenge.这样做并考虑所有场景是一个相当大的挑战。 There is a CocoaPod library that could help with this.有一个 CocoaPod 库可以帮助解决这个问题。 Have a look at EVReflection You could create a dictionary from your object and an object from your dictionary.看看EVReflection你可以从你的对象创建一个字典,从你的字典创建一个对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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