1

I have two enum's for the table. In the 1st, I list the headings for the first section, in the 2nd, I list the icons for the first section. How can I combine them into one enum

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")!
        }
    }
}
JiosDev
  • 324
  • 3
  • 11

2 Answers2

1

You can use like this.

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")!)
        }
    }
}
yasinkbas
  • 315
  • 3
  • 10
0

Try this :

enum SectionTitle {
case one(identifier : String, image : String)
case two(identifier : String, image : String)
}
Shahzaib Qureshi
  • 989
  • 8
  • 13
  • This doesn't really work. Associated values aren't constants (that's the whole point), so nothing would stop you from writing `.one(identifier: "id1", image: "img2.png")`, `.two(identifier: "id2", image: img1.png)`, or any other kind of non-sense like that – Alexander Feb 03 '20 at 16:03
  • he just wants to combine them. and nothing else. – Shahzaib Qureshi Feb 03 '20 at 16:05
  • From the examples, it looks like he's trying to associate one case with a ***matching*** title/image, and a second case with a second ***matching*** title/image. – Alexander Feb 03 '20 at 18:13