Thursday, 15 January 2015

ios - A Switch With A String Enum Is Not Being Recoginized -



ios - A Switch With A String Enum Is Not Being Recoginized -

i have cifilter want effects to. have enum of type string

enum filtertype:string{ case sepiatone = "cisepiatone" case falsecolor = "cifalsecolor" }

but if seek below code, enum not beingness treated string

private class func createfilter(inout filter:cifilter!,filtername:filtertype) { switch filtername{ case .sepiatone: filter = cifilter(name: filtername) //error "extra argument 'name' in phone call default: println("filter name not match") } }

the error message means not seeing enum type string , such thinks i'm using wrong init.

do know wrong switch statement?

you have utilize rawvalue property obtain raw value enum:

filter = cifilter(name: filtername.rawvalue)

just completeness, can utilize initializer obtain enum case raw value:

var filtertype = filtertype(rawvalue: "cisepiatone")

addendum: think using unnecessary switch in filtername() method. if filtertype enum contains valid filters only, variable of filtertype type cannot contain value not 1 of them. in sentiment implementation should work:

private class func createfilter(inout filter:cifilter!,filtername:filtertype) { filter = cifilter(name: filtername.rawvalue) }

moreover, using parameter homecoming value not recommended, unless have reason - can allow function homecoming filter:

func createfilter(filtername:filtertype) -> cifilter { homecoming cifilter(name: filtername.rawvalue) }

last, can rid of function , add together ciimage extension:

extension cifilter { convenience init (filtertype: filtertype) { self.init(name: filtertype.rawvalue) } } allow filtertype = filtertype.falsecolor allow filter = cifilter(filtertype: filtertype)

isn't simpler?

note: if are, me, using xcode 6.0.x, replace filtername.rawvalue filtername.toraw() , filtertype(rawvalue: filtername) filtertype.fromraw(filtername).

ios xcode swift enums switch-statement

No comments:

Post a Comment