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.
- 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
Post a Comment