【问题标题】:How to use enum to get string value如何使用枚举获取字符串值
【发布时间】:2019-12-24 06:22:25
【问题描述】:

实际上我正在尝试将 API JSON 数据填充到 tableView 但我不能将枚举用作字符串。

struct Results: Codable {
    let container_id : Int?
    let container_number : String?
    let shipment_id : Int?
    let bill_of_lading : BillOfLading
    let eta : String?
    let discharge_port_id : Int?
    let discharge_port_name : String?
    let consignee_id : Int?
    let consignee_name : String?
    let customer_id : Int?
    let customer_name : String?
    let shipment_status : String?
    let container_status : String?
    let user_id : Int?
    let user_display_name : String?
    let commodities : [Commodities]
    let all_sold : Bool?
    let quantities : Quantities
    let sales : [String]?
    let sales_report : Sales_report
    let commodities_summary : String?
    let commodity_quantity_summary : String?
    let commodities_summary_en : String?
    let commodity_quantity_summary_en : String?
}

enum BillOfLading: Codable {
    case integer(Int)
    case string(String)

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode(Int.self) {
            self = .integer(x)
            return
        }
        if let x = try? container.decode(String.self) {
            self = .string(x)
            return
        }
        throw DecodingError.typeMismatch(BillOfLading.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for BillOfLading"))
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .integer(let x):
            try container.encode(x)
        case .string(let x):
            try container.encode(x)
        }
    }
}

【问题讨论】:

  • 欢迎来到 StackOverflow。顺便说一句,试试dataa.bill_of_lading.rawValue
  • 我试过了,但我得到的 Value of type 'BillOfLading' has no member 'rowValue'
  • 不相关,因为这是 Swift,我可能建议使用 camelCase 作为属性名称。在解码时,我会将JSONDecoderkeyDecodingStrategy 设置为.convertFromSnakeCase。同样,如果编码,我会将JSONEncoderkeyEncodingStrategy 设置为.convertToSnakeCase。我们不应该让 API 的密钥约定支配我们的属性命名约定。

标签: ios arrays swift uitableview cell


【解决方案1】:

你可以定义一个计算属性,例如

extension BillOfLading {
    var stringValue: String {
        switch self {
            case .integer(let value): return String(value)
            case .string(let value): return value
        }
    }
}

那你就可以了

cell.bolLabel.text = data.bill_of_lading.stringValue

【讨论】:

    【解决方案2】:

    您正在使用cell.bolLable.text = dataa.bill_of_lading 其中bill_of_lading 属于BillOfLading 类型, 和 bolLable.text 仅接受字符串类型。

    试试cell.bolLable.text = dataa.bill_of_lading.string

    【讨论】:

    • 当我尝试使用 cell.bolLable.text = dataa.bill_of_lading.string 时出现此错误 Enum case 'string' cannot be used as an instance member
    【解决方案3】:

    这是一个对我来说很好的解决方法

    // MARK: - Imagefile
    struct Imagefile : Codable
    {
    let large, thumb: String?
    let pageName: String?
    let pageKey: BookType?
    
    enum CodingKeys: String, CodingKey {
        case large, thumb
        case pageName = "page_name"
        case pageKey = "page_key"
        }
    }
    
    enum BookType: Codable {
    case int(Int)
    case string(String)
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode(Int.self) {
            self = .int(x)
            return
        }
        if let x = try? container.decode(String.self) {
            self = .string(x)
            return
        }
        throw DecodingError.typeMismatch(BookType.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for BookType"))
    }
    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .int(let x):
            try container.encode(x)
        case .string(let x):
            try container.encode(x)
        }
    }
    

    }

    这里我使用的是 BookType。

    【讨论】:

      猜你喜欢
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多