1

I have a CIFilter I want to do some effects to. I have an enum of type String

enum FilterType:String{
        case Sepiatone = "CISepiatone"
        case FalseColor = "CIFalseColor"
    }

But if I try the below code, the enum is NOT being treated as a string

 private class func createFilter(inout filter:CIFilter!,filterName:FilterType)
    {
        switch filterName{
        case .Sepiatone:
            filter = CIFilter(name: filterName) //ERROR "Extra argument 'name' in call
        default:
            println("Filter name is not a match")
        }
    }

The error message means its not seeing my enum type as a string and as such thinks I'm using the wrong init.

Do you know what is wrong with my switch statement?

1 Answer 1

2

You have to use the rawValue property to obtain the raw value from an enum:

filter = CIFilter(name: filterName.rawValue)

Just for completeness, similarly you can use an initializer to obtain an enum case from a raw value:

var filterType = FilterType(rawValue: "CISepiatone")

Addendum: I think you are using an unnecessary switch in your filterName() method. If the FilterType enum contains valid filters only, a variable of FilterType type cannot contain a value that is not one of them. So in my opinion this implementation should work:

private class func createFilter(inout filter:CIFilter!,filterName:FilterType)
{
    filter = CIFilter(name: filterName.rawValue)
}

Moreover, using a parameter as a return value is not recommended, unless you have a good reason for that - you can just let the function return the filter:

func createFilter(filterName:FilterType) -> CIFilter
{
    return CIFilter(name: filterName.rawValue)
}

Last, you can just get rid of that function and add a CIImage extension:

extension CIFilter {
    convenience init (filterType: FilterType) {
        self.init(name: filterType.rawValue)
    }
}

let filterType = FilterType.FalseColor
let filter = CIFilter(filterType: filterType)

Isn't it just simpler?


NOTE: if you are, like me, are using Xcode 6.0.x, replace filterName.rawValue with filterName.toRaw() and FilterType(rawValue: filterName) with FilterType.fromRaw(filterName).

Sign up to request clarification or add additional context in comments.

5 Comments

Side note: In Xcode 6.1, toRaw() and fromRaw() have been replaced by a property and a constructor, see stackoverflow.com/questions/25919075/….
I'm still using 6.0.1 - but thanks for that, I'm updating my answer accordingly
Great answer thank you. Regarding the return type, yes I normally do that. However I have created a set of classes called 'processors', and their job it take an object and mutate it. So to me it seems logical that I pass it an image, and the processor mutates it. Or would you suggest otherwise?
No, I don't have any suggestion, without a proper understanding of what your processors do. But that looks like a good reason for having a method with that signature.
I love how helpful the Stack community is! Make every answer count, I love it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.