简体   繁体   English

这个枚举是两种不同的类型吗?

[英]Is this enum of two different types?

I'm following a tutorial and I noticed the author(s) declared this enum with what looks like to be multiple types. 我正在看一个教程,我注意到作者用看起来像是多种类型的声明了这个枚举。 Based on what I've read online from the Swift Standard Library, I understand enums can be of a certain type and enums do not support inheritance. 根据我从Swift标准库在线阅读的内容,我了解到枚举可以是某种类型,并且枚举不支持继承。 Is this enum of both String and CodingKey type? 这个枚举类型是否为String和CodingKey? Or is the name case a String type and the items case a CodingKey type? 还是名称大小写是String类型,项目大小写是CodingKey类型?

private enum CodingKeys: String, CodingKey {
        case name
        case items
    }

This isn't inheritance, it's two things: 这不是继承,是两件事:

  1. A raw value clause . 原始价值条款 This is a special form that speficies the "backing value" used to represent the enum 's cases. 这是一种特殊形式,它专门用来表示enum案例的“后备值”。 In this case, it's String . 在这种情况下,它是String When an enum has chosen to have a String raw value, but the case doesn't specify a raw value, the name of the case is implicitly assumed to be the raw value of the case. 当枚举选择具有String原始值,但case未指定原始值时,将隐式假定案例的名称为case的原始值。
  2. A protocol conformance clause, which declares that this enum conforms to CodingKey . 协议一致性子句,声明该枚举符合CodingKey

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

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