简体   繁体   English

在Swift中存储Enum类型

[英]Storing Enum type in Swift

I am working on Enum serialization for my library ( https://github.com/JiriTrecak/Warp ) and I got little stuck on storing the enum type in the property (I need to know that type so I can serialize / deserialize it on demand). 我正在为我的库( https://github.com/JiriTrecak/Warp )进行Enum序列化,并且在将枚举类型存储在属性中时我几乎没有任何卡住(我需要知道该类型,以便可以在其上序列化/反序列化它)需求)。

I have a struct that contains description of all the properties that you can have, including their type, remote keys etc., and I'd like to store one more optional information if that is enum, which is the Enum type to be created when the key is found (say, Gender). 我有一个结构,其中包含您可以拥有的所有属性的描述,包括它们的类型,远程键等,并且我想存储一个可选的信息(如果是枚举),即在创建时要创建的Enum类型。找到了密钥(例如,性别)。

I've tried two approaches, both of which are not usable: 我尝试了两种方法,两种方法都不可用:

A) Declare the generic type in the container definition A)在容器定义中声明通用类型

public struct WRPProperty<T: RawRepresentable> {
    var enumType : T?

    public init(remote : String, enumType: T) {
        self.enumType = enumType
    }
}

This works, the problem with this approach is that I don't want every WRPProperty to have enum. 这行得通,这种方法的问题是我不希望每个WRPProperty都有枚举。 By declaring it like this, it forces user to always add data type when creating the property, which is unwanted behavior. 通过这样声明,它迫使用户在创建属性时总是添加数据类型,这是不希望的行为。 I can also create object that would be WRPEnumProperty, but that is not usable due to how the library handles those definitions and how user defines them. 我还可以创建将为WRPEnumProperty的对象,但是由于该库如何处理这些定义以及用户如何定义它们而无法使用。

B) Declare the generic type in the init method B)在init方法中声明泛型类型

public struct WRPProperty {
    var enumType : RawRepresentable

    public init<T: RawRepresentable>(remote : String, enumType: T) {
        self.enumType = enumType
    }
}

Which does not work, because RawRepresentable can only be used as generic constraint. 这不起作用,因为RawRepresentable只能用作通用约束。

So my question is, how can I store the enum type, so I can then create any Enum of that type at any point? 所以我的问题是,如何存储枚举类型,以便可以在任何时候创建该类型的任何Enum? (Also, I am using Swift 2.2) (此外,我正在使用Swift 2.2)

In your example (B) even if the compiler allowed it, there would be no way to guarantee that the init enumType was the same as the enumType property. 在示例(B)中,即使编译器允许,也无法保证initType与enumType属性相同。

The only solution that I can think of in this situation would be to ignore type safety and use Any 在这种情况下,我能想到的唯一解决方案是忽略类型安全性并使用Any

public struct WRPProperty {
    var enumType:Any?
    public init<T:RawRepresentable>(remote : String, enumType: T) {
        self.enumType = enumType
    }
}

unless you can find some clever way of wrapping your values or employing a more sophisticated type erasure . 除非您找到某种巧妙的方法来包装您的价值观或采用更复杂的类型擦除 This provides you with the dynamic ability you are looking for (although the best solution is probably to restructure your approach so that the enum Type can be set at instantiation if at all possible). 这为您提供了所需的动态能力(尽管最好的解决方案可能是重组您的方法,以便在可能的情况下可以将枚举类型设置为实例化)。

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

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