ios - Use enum swift issue -


i have enum:

enum vaxsettingscells : int   {     case switchmodecell = 0     case switchercell = 1     case newprogramcell = 2     case createdprogramcell = 3   } 

which use in uitableview delegate:

override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell   {     let cellid = vaxsettingscells(rawvalue: indexpath.row)      switch cellid {     case .switchmodecell:       let cell = thetableview.dequeuereusablecellwithidentifier(vaxsettingsswitchmodecell.reusablecellidentifier()) as! vaxsettingsswitchmodecell       cell.delegate = self       return cell 

but error:

enum case 'switchmodecell' not found in type 'vaxsettingsviewcontroller.vaxsettingscells?' 

how rid of error? can use raw values int , work want use enum data instead don't want use default case of switch.

you first need unwrap vaxsettingscells return optional value.

if define enumeration raw-value type, enumeration automatically receives initializer takes value of raw value’s type (as parameter called rawvalue) , returns either enumeration case or nil

if let cellid = vaxsettingscells(rawvalue: indexpath.row) { switch cellid {  case .switchmodecell:   // whatever want here default: break  }  } 

and in example above, vaxsettingscells has implicit raw value of 0 switchmodecell, , on. don't need give explicitly. use

enum vaxsettingscells : int {  case switchmodecell  case switchercell  case newprogramcell  case createdprogramcell } 

you access raw value of enumeration case rawvalue property.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -