用于序列化键的Struct或Enum?

时间:2022-06-04 00:15:47

Is there any reason why Apple prefers using structs over enums in the Lister demo for declaring keys for serialization? Is there might be some benefits?

是否有任何理由为什么Apple更喜欢在Lister演示中使用结构来声明序列化的键?可能会有一些好处吗?

For example:

例如:

private struct SerializationKeys {
    static let text = "text"
    static let uuid = "uuid"
    static let completed = "completed"
    ...
    //duplicated key!
    static let descriptionText = "text"
}

Here we might have potential duplicates for keys. It's not a big question for small objects (don't forget copy/paste :)), but for large objects with tens fields it can be a real problem.

在这里,我们可能有重复的密钥。对于小对象来说这不是一个大问题(不要忘记复制/粘贴:)),但对于具有数十个字段的大对象,这可能是一个真正的问题。

With enum we don't have such a problem:

使用枚举我们没有这样的问题:

    private enum SerializationKeys : String {
    case text = "text"
    case uuid = "uuid"
    case completed = "completed"
    //...
    case descriptionText = "text"
    //here we have compiler's warning: Raw value for enum case is not unique
}

Will be happy to hear some thoughts on this.

很高兴听到一些关于此的想法。

2 个解决方案

#1


6  

I do the same thing, sometimes, and here's why.

我有时做同样的事情,这就是原因。

With a struct, my values are directly available: so, if SerializationKeys is a struct, then SerializationKeys.text is a string.

使用结构,我的值可以直接使用:因此,如果SerializationKeys是结构,则SerializationKeys.text是一个字符串。

But with an enum, the enum is the value. If SerializationKeys is an enum, then SerializationKeys.text is not a string; it's an enum. If I want the string, I have to fetch it explicitly, as the enum's rawValue. Sometimes, that's just too nutty. On the other hand, if it's acceptable, or if there's another reason why this makes a good enum, then fine, I'll use an enum.

但是对于枚举,枚举就是价值。如果SerializationKeys是枚举,则SerializationKeys.text不是字符串;这是一个枚举。如果我想要字符串,我必须明确地获取它,作为枚举的rawValue。有时候,这太疯狂了。另一方面,如果它是可以接受的,或者如果有另一个原因使它成为一个好的枚举,那么很好,我将使用枚举。

To put it another way: if this is just a glorified namespace for some constants, a struct with static members seems simplest. An enum is for a switch, i.e. something that needs to exist in exactly one of several possible states.

换句话说:如果这只是某些常量的美化命名空间,那么具有静态成员的结构似乎最简单。枚举用于切换,即需要以几种可能状态中的一种存在的东西。

#2


3  

Apple's reason for choosing a struct here seems to be purely semantic. SerializationKeys.descriptionText is a property. SerializationKey.DescriptionText is a type. And it is kind of semantically weird to use a type as a key.

Apple在这里选择结构的原因似乎纯粹是语义上的。 SerializationKeys.descriptionText是一个属性。 SerializationKey.DescriptionText是一种类型。使用类型作为键是一种语义上的怪异。

True, in this particular instance the SerializationKey.DescriptionText type happens to have a "raw" value associated with it. But as I understand it, raw values are really only intended to be used as a sort of "bridging layer" layer between C enums. Using it for keys like this is kind of a hack.

是的,在这个特定的实例中,SerializationKey.DescriptionText类型恰好具有与之关联的“原始”值。但据我了解,原始值实际上只是用作C枚举之间的一种“桥接层”层。将它用于像这样的键是一种黑客攻击。

#1


6  

I do the same thing, sometimes, and here's why.

我有时做同样的事情,这就是原因。

With a struct, my values are directly available: so, if SerializationKeys is a struct, then SerializationKeys.text is a string.

使用结构,我的值可以直接使用:因此,如果SerializationKeys是结构,则SerializationKeys.text是一个字符串。

But with an enum, the enum is the value. If SerializationKeys is an enum, then SerializationKeys.text is not a string; it's an enum. If I want the string, I have to fetch it explicitly, as the enum's rawValue. Sometimes, that's just too nutty. On the other hand, if it's acceptable, or if there's another reason why this makes a good enum, then fine, I'll use an enum.

但是对于枚举,枚举就是价值。如果SerializationKeys是枚举,则SerializationKeys.text不是字符串;这是一个枚举。如果我想要字符串,我必须明确地获取它,作为枚举的rawValue。有时候,这太疯狂了。另一方面,如果它是可以接受的,或者如果有另一个原因使它成为一个好的枚举,那么很好,我将使用枚举。

To put it another way: if this is just a glorified namespace for some constants, a struct with static members seems simplest. An enum is for a switch, i.e. something that needs to exist in exactly one of several possible states.

换句话说:如果这只是某些常量的美化命名空间,那么具有静态成员的结构似乎最简单。枚举用于切换,即需要以几种可能状态中的一种存在的东西。

#2


3  

Apple's reason for choosing a struct here seems to be purely semantic. SerializationKeys.descriptionText is a property. SerializationKey.DescriptionText is a type. And it is kind of semantically weird to use a type as a key.

Apple在这里选择结构的原因似乎纯粹是语义上的。 SerializationKeys.descriptionText是一个属性。 SerializationKey.DescriptionText是一种类型。使用类型作为键是一种语义上的怪异。

True, in this particular instance the SerializationKey.DescriptionText type happens to have a "raw" value associated with it. But as I understand it, raw values are really only intended to be used as a sort of "bridging layer" layer between C enums. Using it for keys like this is kind of a hack.

是的,在这个特定的实例中,SerializationKey.DescriptionText类型恰好具有与之关联的“原始”值。但据我了解,原始值实际上只是用作C枚举之间的一种“桥接层”层。将它用于像这样的键是一种黑客攻击。