条件协议一致性?

时间:2016-05-21 06:04:30

标签: swift generics constraints protocols

我想做这样的事情:

class SomeClass<Element> { }
extension SomeClass: SomeProtocol where Element: String { }

它告诉我:

  

类型扩展&#34; SomeClass&#34; with constraints不能有继承子句。

我本可以宣誓,这是protocol / extension / generic / associatedtype范例的面包和黄油功能之一。还有其他方法可以实现吗?

2 个答案:

答案 0 :(得分:1)

这是在Swift 4.1中实现的。

答案 1 :(得分:0)

作为保罗,你现在可以在Swift 4中做到这一点

protocol Nameable {
    var name:String {get set}
}
func createdFormattedName<T:Nameable>(_ namedEntity:T) -> String where T:Equatable {
    //Only entities that conform to Nameable which also conform to equatable can call this function
    return "This things name is " + namedEntity.name
}
相关问题