如何调用枚举作为实例变量并从中获取字符串值?

时间:2019-02-08 10:16:52

标签: swift enums dart flutter enumeration

我在dart中有一个类,该类将枚举作为实例变量并且必须返回字符串。代码如下:

enum ModelType {
  catalog,
  catalogItem,
  merchant
}

class BannerBodyBannerable {
  ModelType type;
  int id;

  BannerBodyBannerable({this.id, this.type});
}

我可以在dart中这样调用吗:type.catalog并获取字符串值?

我有一个快捷文件,例如:

public struct BannerBodyBannerable: Codable {

    public enum ModelType: String, Codable { 
        case catalog = "CATALOG"
        case catalogItem = "CATALOG_ITEM"
        case merchant = "MERCHANT"
    }
    public var _id: Int?
    public var type: ModelType?
    public init(_id: Int?, type: ModelType?) { 
        self._id = _id
        self.type = type
    }
    public enum CodingKeys: String, CodingKey { 
        case _id = "id"
        case type
    }

}

枚举的目的与上面的文件完全一样,因此将枚举作为实例变量并获取字符串值,这在dart中是可能的吗?

0 个答案:

没有答案