【问题标题】:Combine enum's in swift快速组合枚举
【发布时间】:2020-05-19 09:22:08
【问题描述】:

我有两个枚举表。在第一个中,我列出了第一部分的标题,在第二个中,我列出了第一部分的图标。 如何将它们组合成一个枚举

enum cellSectionOne:Int, CaseIterable
{
    case cellOne
    case cellTwo

    var titleCellSectionOne:String
    {
        switch self {
        case .cellOne:
            return  "cellOne"
        case .cellTwo:
            return  "cellTwo"

        }
    }

}

enum cellIconSectionOne:Int, CaseIterable {

    case cellOneIcon
    case cellTwoIcon

    var icon: UIImage {
        switch self {
        case .cellOneIcon:
            return UIImage(named: "iconOne.png")!
        case .cellTwoIcon:
            return UIImage(named: "iconTwo.png")!
        }
    }
}

【问题讨论】:

标签: ios swift enums switch-statement


【解决方案1】:

试试这个:

enum SectionTitle {
case one(identifier : String, image : String)
case two(identifier : String, image : String)
}

【讨论】:

  • 这真的行不通。关联值不是常量(这就是重点),所以没有什么能阻止你写.one(identifier: "id1", image: "img2.png").two(identifier: "id2", image: img1.png) 或任何其他类似的废话
  • 他只是想把它们结合起来。没有别的。
  • 从示例中,他似乎试图将一个案例与 匹配 标题/图像相关联,并将第二个案例与第二个 匹配标题/图像。
【解决方案2】:

你可以这样使用。

enum CellSection: Int {
    case one
    case two

    var id: String {
        return value.id
    }

    var icon: UIImage {
        return value.icon
    }

    private var value: (id: String, icon: UIImage) {
        switch self {
        case .one:
            return ("cellOne", UIImage(named: "iconOne.png")!)
        case .two:
            return ("cellTwo", UIImage(named: "iconTwo.png")!)
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多