一般使用具有关联类型的 swift 协议

Using swift protocols with associated types generically

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

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

以下代码不起作用,因为当 Decoder 具有关联类型时,您不能将其用作 return 类型。

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

有什么方法可以让编译器强制执行 "all Decoders should have a decode() method according to their associatedtype"?

我尝试过使用通用的 superclass,但它要求我为 decode() 提供一个方法体,如果您的 return 类型不是可选的,这将非常粗糙。

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 ... */
}

如果你使 Model 成为一个协议,那么你可以 return 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()
    }
    // ...
}