iOS Swift: CIDetectorTypeRectangle is not convertible

346 views Asked by At

I want to use CIDetector class to detect rectangles but I cannot create the instance of this class. I get the error message:

'(ofType: String, context: CIContext?, options: [String : AnyObject]?) -> CIDetector' is not convertible to '(ofType: String, context: CIContext?, options: [String : AnyObject]?) -> CIDetector?'

I use this code:

private lazy var rectangleDetector: CIDetector? = {
    let options: [String : AnyObject] = [
        CIDetectorAccuracy: CIDetectorAccuracyHigh,
        CIDetectorMinFeatureSize: 0.5,
        CIDetectorAspectRatio: 0.7
    ]

     return CIDetector(ofType: CIDetectorTypeRectangle, context: nil, options: options)
}()

I use XCode 8.2.1 and Swift 2.3. There wasn't any error when I used it in XCode 7 with Swift 2.2. Does anybody know what is wrong here?

1

There are 1 answers

0
Neil Horton On

It looks like the returned detector is now optional. Try unwrapping it before using it.

guard let detector = CIDetector(ofType: CIDetectorTypeRectangle, context: nil, options: nil) else {
  return //Or better yet throw error
}

//Do something with detector