2016-06-23 12 views
1

私はSwiftでクラスモデリングに関する質問をしています。私はそれぞれが同じタスク(以下の私の例では、Decoding)をするクラスの範囲を持っていますが、それらは特殊化されており、それぞれ異なるタイプのオブジェクトを生成します。関連するタイプの一般的な迅速なプロトコルを使用する

場合によっては、getGeneralInfo()getDecoderForIdentifier()などの一般的なデコーダについて話したいと思うこともあります。私がデコード操作を行っている場所などの他の場合では、クラスを直接インスタンス化するか、as?を使用します。

Decoderを関連付けられた型がある場合は戻り値の型として使用できないため、次のコードは機能しません。

私の解決策は、プロトコルからdecode()を削除し、各クラスに独自の実装をさせ​​ることです。私はその後、具体的なクラスを必要な場所に直接インスタンス化する必要があります。これは実行可能ですが、それは私を悲しくします。

コンパイラに「関連するタイプに応じてすべてのデコーダがdecode()メソッドを持っている必要があります」というようにこれを再実行する方法はありますか?

一般的なスーパークラスを使用しようとしましたが、decode()のメソッド本体を用意する必要があります。返り値の型がオプションでない場合はかなりぎりぎりです。あなたがModelプロトコルを作る場合、それは、関連するタイプを必要としないので

protocol Decoder { 
    associatedtype Model 
    func getGeneralInfo() -> GeneralInfo 
    func decode(sourceData: Data) -> Model 
} 

// This return type is not allowed because Decoder has an associated type 
func getDecoderForIdentifier(id: String) -> Decoder { 
    if id == "mp3" { 
     return Mp3Decoder() 
    } 
    if id == "wave" { 
     return WaveDecoder() 
    } 
    /* ... */ 
} 

class Mp3Decoder: Decoder { 
    typealias Model = Mp3Info 

    func getGeneralInfo() -> GeneralInfo { 
     let info = GeneralInfo() 
     /* ... */ 
     return info 
    } 

    func decode(sourceData: Data) -> Model { 
     let result = Mp3Info() 
     /* ... */ 
     return result 
    } 
} 

class WaveDecoder: Decoder { 
    typealias Model = WaveInfo 

    /* ... similar to mp3 ... */ 
} 

答えて

1

あなたはDecoderを返すことができます。

protocol Model { 
    // ... 
} 

protocol Decoder { 
    func getGeneralInfo() -> GeneralInfo 
    func decode(sourceData: Data) -> Model 
} 

class Mp3Decoder: Decoder { 

    func getGeneralInfo() -> GeneralInfo { 
     let info = GeneralInfo() 
     // ... 
     return info 
    } 

    func decode(sourceData: Data) -> Model { 
     let result = Mp3Info() 
     // ... 
     return result 
    } 

} 

func getDecoderForIdentifier(id: String) -> Decoder { 
    if id == "mp3" { 
     return Mp3Decoder() 
    } 
    // ... 
} 
+0

ああ、それは賢いです!私はまだこの構造が良いアイデアだと自分自身を説得していますが、あなたが間違いなく私が持っている最大の問題を解決した、ありがとう。 – Tom

関連する問題