2016-12-21 12 views
2

これはなぜ機能しないのですか?Swift - CIDetectorのサブクラスの便利なイニシャライザが動作しません

import CoreImage 

class RectDetector: CIDetector { 

    convenience init?(aspectRatio: Float) { 
     let options: [String : Any] = [CIDetectorAccuracy : CIDetectorAccuracyHigh, 
             CIDetectorAspectRatio : NSNumber(value: aspectRatio)] 
     self.init(ofType: CIDetectorTypeRectangle, context: nil, options: options) 
    } 

} 

私はエラー"メンバーinit(aspectRatio:)へのあいまいな参照" を取得しています。私はこのようなアスペクト比のデフォルト値を追加しようと

は:

import CoreImage 

class RectDetector: CIDetector { 

    convenience init?(aspectRatio: Float = 1.0) { 
     let options: [String : Any] = [CIDetectorAccuracy : CIDetectorAccuracyHigh, 
             CIDetectorAspectRatio : NSNumber(value: aspectRatio)] 
     self.init(ofType: CIDetectorTypeRectangle, context: nil, options: options) 
    } 

} 

が、私はエラーを取得する「それを呼び出すために渡される引数は、引数を取りません」。

これはバグですか?なぜ私はCIDetectorを既存のイニシャライザに連鎖しているカスタムコンビネーションイニシャライザでサブクラス化できないのですか?

P.S.私はXcode 8.2(8C38)を使用しています

答えて

2

Swiftは便利な初期化子(Class Factory Methods and Convenience Initializers)としていくつかのクラスメソッドをインポートします。

あなたの場合、init(ofType:context:options:)は、Objective-C + detectorOfType:context:options:のクラスメソッドCIDetectorです。

このような便利な初期化子は、サブクラスでは使用できません。このようなクラスのファクトリメソッドは、常にクラスのインスタンスを作成し、定義したサブクラスのインスタンスを作成することはできません。

クラスファクトリメソッドに基づいた便利な初期化子を使用して別の便利な初期化子を提供する場合は、拡張が必要な​​場合があります。

extension CIDetector { 

    convenience init?(rectDetectorWithAspectRatio aspectRatio: Float) { 
     let options: [String : Any] = [CIDetectorAccuracy : CIDetectorAccuracyHigh, 
             CIDetectorAspectRatio : NSNumber(value: aspectRatio)] 
     self.init(ofType: CIDetectorTypeRectangle, context: nil, options: options) 
    } 

} 

ところで、診断メッセージは、一見完全スウィフトを使用してプログラマ側から壊れています。あなたはそれについてBug Reportを送ることができます。

関連する問題