appzYourLife appzYourLife - 5 months ago 8
Swift Question

Cannot call value of non-function type 'CIImage?'

This code does compile perfectly fine

let ciImage = CIImage(image:UIImage())


however when I move it into an extension of
UIImage


extension UIImage {
func foo() {
let ciImage = CIImage(image:UIImage()) // compile error
}
}


I get the following compile error


Cannot call value of non-function type 'CIImage?'


Why?




Tested with Xcode Playground 7.1.1. and Swift 2.1

Answer

It's because, once inside UIImage, the term CIImage is seen as the CIImage property of UIImage, due to implicit self as message recipient — in other words, Swift turns your CIImage into self.CIImage and it's all downhill from there.

You can solve this by disambiguating thru Swift's use of module namespacing:

extension UIImage {
    func foo() {
        let ciImage = UIKit.CIImage(image:UIImage())
    }
}

EDIT In Swift 3 this problem will go away, because all properties will start with small letters. The property will be named ciImage, and there will be no confusion with the CIImage class.

Comments