快速合并枚举

时间:2020-02-03 15:29:13

标签: ios swift enums switch-statement

我的桌子上有两个枚举。在第一部分中,我列出了第一部分的标题,在第二部分中,我列出了第一部分的图标。 如何将它们合并为一个枚举

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")!
        }
    }
}

2 个答案:

答案 0 :(得分:1)

尝试一下:

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

答案 1 :(得分:1)

您可以这样使用。

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")!)
        }
    }
}
相关问题