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

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -