2017-01-03 15 views
1

我想使用CIDetector類來檢測矩形,但我無法創建此類的實例。我得到的錯誤信息:iOS Swift:CIDetectorTypeRectangle不可轉換

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

我用這個代碼:

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) 
}() 

我使用的XCode 8.2.1和SWIFT 2.3。當我在Swift 2.2的XCode 7中使用它時沒有任何錯誤。有人知道這裏有什麼問題嗎?

+0

你確定你在使用Swift 2.3還是Swift 3? –

+0

是的,我使用Swift 2.3,並將「使用傳統Swift語言版本」設置爲YES。 –

+0

嘗試清理構建,因爲對我來說它使用Xcode 8和Swift 2.3或者如果仍然無效,請使用它添加更多的代碼 –

回答

1

它看起來像返回的檢測器現在是可選的。在使用之前嘗試解開它。

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

//Do something with detector 
+0

我試過了,它沒有幫助。 –