您的IdentifierProtocol 是RawRepresentable,其中有associatedType。而且这样的协议不能“像具体类型一样”使用。
这基本上就是编译错误所说的:
错误:协议 'A_IdentifierProtocol' 只能用作通用约束,因为它具有 Self 或关联的类型要求
建议您阅读有关您遇到的错误的说明,而不是重复信息:https://stackoverflow.com/a/36350283/2378431
如果您想解决此错误并且仍然让您的代码正常工作而没有任何明显差异(从使用角度来看),您可以为每个 X_IdentifierProtocol 定义一个方法,如下所示:
protocol IdentifierProtocol: Equatable, RawRepresentable {}
protocol A_IdentifierProtocol: IdentifierProtocol {}
protocol B_IdentifierProtocol: IdentifierProtocol {}
func myFunc<I: A_IdentifierProtocol>(identifier: I) where I.RawValue == String {
print("A: \(identifier.rawValue)")
}
func myFunc<I: B_IdentifierProtocol>(identifier: I) where I.RawValue == String {
print("B: \(identifier.rawValue)")
}
缺点是,对于每个X_IdentifierProtocol,您需要提供一个方法实现,如果您想拥有一段基于IdentifierProtocol 的共享代码,可能会引入一些代码重复。
另一种方法:如果您真的想要单个功能,则不能将 IdentifierProtocol 与关联类型联系起来。但是,您可以对泛型函数有多种类型约束,如下所示:
protocol IdentifierProtocol {}
protocol A_IdentifierProtocol: IdentifierProtocol {}
protocol B_IdentifierProtocol: IdentifierProtocol {}
func myFunc<I: IdentifierProtocol & Equatable & RawRepresentable>(identifier: I) where I.RawValue == String {
if identifier is A_IdentifierProtocol {
print("A: \(identifier.rawValue)")
}
if identifier is B_IdentifierProtocol {
print("A: \(identifier.rawValue)")
}
}
class MyClassA: A_IdentifierProtocol, RawRepresentable, Equatable {...}
class MyClassB: B_IdentifierProtocol, RawRepresentable, Equatable {...}
但即使这样也不是完美的,也不能完全满足您的要求。
底线是你无法用 Swift 3 完全实现你想要的。