dictionary - Accessing String key to display fetch image (Swift) -


i trying correspond "major" dictionary picture associated each major displays in cell.

import foundation  class classrostermodel {     var studentsroster = [dictionary<string, string>] ()     init () {         studentsroster.append(["name": "kaz, alex", "number" : "s0834347", "major" : "se"])         studentsroster.append(["name": "o'rore, ryan", "number" : "s0835357", "major" : "se"])         studentsroster.append(["name": "lote, lote", "number" : "s0835357", "major" : "se"])         studentsroster.append(["name": "flora, nico", "number" : "s0748324", "major" : "ma"])     } }  override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecellwithidentifier("studentcell", forindexpath: indexpath)      // configure cell...     cell.textlabel?.text = studentslist[indexpath.row]["name"]     cell.detailtextlabel?.text = studentslist[indexpath.row]["number"]     print("student's name: \(studentslist[indexpath.row]["name"])")     print("student's number: \(studentslist[indexpath.row]["number"])")      return cell 

you can make dictionary map "major" image, example:

let images = [     "se": uiimage(named: "se-image.png"),     "md": uiimage(named: "another-image.png") ] let major = studentslist[indexpath.row]["major"] if let image = images[major] {     cell.imageview?.image = image } 

i'm gonna nice you, , provide better example of how write swift code:

enum major: string {     case se     case ma      var image: uiimage? {         return uiimage(named: self.rawvalue)     } } struct student {     let name: string     let number: string     let major: major }  class sometableviewcontroller: uitableviewcontroller {     let students = [         student(name: "kaz, alex",    number: "s0834347", major: .se),         student(name: "o'rore, ryan", number: "s0835357", major: .se),         student(name: "lote, lote",   number: "s0835357", major: .se),         student(name: "flora, nico",  number: "s0748324", major: .ma)     ]      override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {         let student = students[indexpath.row]          let cell = tableview.dequeuereusablecellwithidentifier("studentcell", forindexpath: indexpath)         cell.textlabel?.text = student.name         cell.detailtextlabel?.text = student.number         cell.imageview?.image = student.major.image         return cell     } }