ios - Using enum string type as dictionary key in swift 2.0 -


i have enum

enum filtertype:string {     case unitsoldfilter = "unitsoldfilter"     case amountfilter = "amountfilter" } 

i want method in want save corresponding value

    func getfilterfortype(filterfor:filterfortype) -> nsdata? {          if let data: nsdata = nsuserdefaults.standarduserdefaults().objectforkey(filterkey) as? nsdata{ return data             }             return nil     } 

but getting error can't use filterkey directly. how can solved.

two things.

  1. in swift 2.0 don't need specify string enum corresponds if they're same string.

so

enum filtertype:string {     case unitsoldfilter = "unitsoldfilter"     case amountfilter = "amountfilter" } 

becomes

enum filtertype:string {     case unitsoldfilter     case amountfilter } 

and inside of method you're going use rawvalue property.

func getfilterfortype(filterfor:filterfortype) -> nsdata? {          if let data: nsdata = nsuserdefaults.standarduserdefaults().objectforkey(filterkey.rawvalue) as? nsdata{ return data             }             return nil     } 

that should trick.


Comments

Popular posts from this blog

html - Difficulties with background-image property -

visual studio code - What does the isShellCommand property actually do and how should you use it? -

ios - Segue not passing data between ViewControllers -