是否有任何理由为什么Apple更喜欢在Lister演示中使用结构来声明序列化的键?可能会有一些好处吗?
例如:
private struct SerializationKeys {
static let text = "text"
static let uuid = "uuid"
static let completed = "completed"
...
//duplicated key!
static let descriptionText = "text"
}
Run Code Online (Sandbox Code Playgroud)
在这里,我们可能有重复的密钥.对于小对象来说这不是一个大问题(不要忘记复制/粘贴:)),但对于具有数十个字段的大对象,这可能是一个真正的问题.
使用枚举我们没有这样的问题:
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
}
Run Code Online (Sandbox Code Playgroud)
很高兴听到一些关于此的想法.