"This class is not suitable for keyword coding" using CoreImage

I am working on a simple macOS command line application in Swift. I created my own CoreImage filter and had problems using it. The code compiles just fine, but when it starts, it exits with the following error:

*** Application termination due to the uncaught exception "NSUnknownKeyException", reason: "[setValue: forUndefinedKey:]: this class is not a key value compatible with the encoding for the key inputImage. '

Any help in this matter would be greatly appreciated. I tried searching for an answer on SO, but since my application does not use storyboards or output (this is a clean command line), unfortunately, I could not help myself.

My program breaks into this line:

filter.setValue(inputImage, forKey: kCIInputImageKey)

Here is the code I'm using:

class CustomFilter:CIFilter {
    var inputImage:CIImage?

    let kernelString = CIKernel(string:
        "kernel vec4 chromaKey( __sample s) { \n" +
            "  vec4 newPixel = s.rgba;" +
            "  newPixel[0] = 0.0;" +
            "  newPixel[2] = newPixel[2] / 2.0;" +
            "  return newPixel;\n" +
        "}"
    )

    override var outputImage:CIImage! {
        guard
            let inputImage = inputImage
        else {
            return nil
        }

        let extent = inputImage.extent

        let blur = kernelString?.apply(
            withExtent: extent,
            roiCallback: {
                (index, rect) in
                return rect
            },
            arguments: [inputImage])

        return blur!.cropping(to: extent)
    }
}

let filter = CustomFilter()

filter.setValue(inputImage, forKey: kCIInputImageKey) // it breaks here

guard
    let result = filter.outputImage
else {
    return nil
}

return result
+4
source share
1 answer

Values ​​that comply with the encoding requirements should be marked as dynamic

dynamic var inputImage : CIImage?

and in Swift 4 even as @objc

@objc dynamic var inputImage : CIImage?
+13
source

Source: https://habr.com/ru/post/1686882/


All Articles