SWIFT 1.2 and CIFIlters

Home / SWIFT 1.2 and CIFIlters

If you happened to develop your own cool CIFilters with Swift, and you updated your XCode to XCode-beta 6.3 you might run into some errors. most likely your code looks like this

class IMGLYNoneFilter : CIFilter {
    var inputImage:CIImage?   

    func outputImage() -> CIImage! {
        if inputImage == nil {
            return CIImage.emptyImage()
        }
        return inputImage!
    }
}

All good all fine, but if you update to Swift 1.2, you’ll get some trouble. The compiler will complain and tell you that you are trying to override a objective-c method, and no, adding the override keyword wont fix it. So now you can override objective-c properties, and that’s exactly what you have to do. In the end, your code should look something like this:

class IMGLYNoneFilter : CIFilter {
    var inputImage:CIImage?   

    override var outputImage: CIImage! {
        get {
            if inputImage == nil {
                return CIImage.emptyImage()
            }
            return inputImage!
        }
    }
}

Tadaaa! This compiles and works as expected.