在Swift中存储Enum类型(Storing Enum type in Swift)

编程入门 行业动态 更新时间:2024-10-22 14:40:58
在Swift中存储Enum类型(Storing Enum type in Swift)

我正在为我的库( https://github.com/JiriTrecak/Warp )开发Enum序列化,我很少停留在属性中存储枚举类型(我需要知道该类型,所以我可以序列化/反序列化它需求)。

我有一个结构,其中包含您可以拥有的所有属性的描述,包括它们的类型,远程键等,如果是枚举,我想再存储一个可选信息,这是当要创建的枚举类型时找到钥匙(比如性别)。

我尝试了两种方法,两种方法都不可用:

A)在容器定义中声明泛型类型

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

这是有效的,这种方法的问题是我不希望每个WRPProperty都有枚举。 通过声明它,它强制用户在创建属性时始终添加数据类型,这是不需要的行为。 我也可以创建WRPEnumProperty的对象,但由于库处理这些定义以及用户如何定义它们,因此无法使用。

B)在init方法中声明泛型类型

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

哪个不起作用,因为RawRepresentable只能用作通用约束。

所以我的问题是,如何存储枚举类型,这样我就可以在任何时候创建任何类型的枚举? (另外,我使用的是Swift 2.2)

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).

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).

I've tried two approaches, both of which are not usable:

A) Declare the generic type in the container definition

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. 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.

B) Declare the generic type in the init method

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.

So my question is, how can I store the enum type, so I can then create any Enum of that type at any point? (Also, I am using Swift 2.2)

最满意答案

在您的示例(B)中,即使编译器允许它,也无法保证init enumType与enumType属性相同。

在这种情况下,我能想到的唯一解决方案是忽略类型安全并使用Any

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

除非你能找到一些巧妙的方法来包装你的价值观或采用更复杂的类型擦除 。 这为您提供了您正在寻找的动态能力(尽管最好的解决方案可能是重构您的方法,以便可以在实例化时设置枚举类型,如果可能的话)。

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.

The only solution that I can think of in this situation would be to ignore type safety and use 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).

更多推荐

本文发布于:2023-08-06 05:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   Enum   Swift   Storing   type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!