一般使用swift协议和相关类型

时间:2016-06-23 05:03:30

标签: swift generics swift-protocols

我有一个使用Swift的类建模问题。我有一系列的类,每个类都执行相同的任务(在我的下面的示例中,解码),但它们是专门的,每个都产生不同类型的对象。

在某些情况下,我希望能够通常谈论我的解码器,例如getGeneralInfo()getDecoderForIdentifier()。在其他情况下,例如我正在进行解码操作,我将直接实例化该类或使用as?

以下代码不起作用,因为当它具有关联类型时,您无法使用Decoder作为返回类型。

我的解决方案是从协议中删除decode()并让每个类只实现自己的。然后,我需要直接在需要它们的地方实例化具体类。这是可行的,但这让我感到难过。

我有什么方法可以重新调整它以使编译器强制执行"所有解码器都应根据其关联类型&#34进行decode()方法;?

我尝试过使用通用超类,但它要求我为decode()提供一个方法体,如果你的返回类型不是可选的,那就太粗糙了。

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 个答案:

答案 0 :(得分:1)

如果您使Model成为协议,那么您可以返回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()
    }
    // ...
}
相关问题