簡體   English   中英

快速結合枚舉

[英]Combine enum's in swift

我有兩個枚舉表。 在第一個中,我列出了第一部分的標題,在第二個中,我列出了第一部分的圖標。 我怎樣才能將它們組合成一個枚舉

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

嘗試這個 :

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

你可以這樣使用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM