1
使用相关类型的协议,我有问题:从协议类型的Swift associatedType - 如何做到这一点?
protocol Searchable{
func matches(text: String) -> Bool
}
protocol ArticleProtocol: Searchable {
var title: String {get set}
}
extension ArticleProtocol {
func matches(text: String) -> Bool {
return title.containsString(text)
}
}
struct FirstArticle: ArticleProtocol {
var title: String = ""
}
struct SecondArticle: ArticleProtocol {
var title: String = ""
}
protocol SearchResultsProtocol: class {
associatedtype T: Searchable
}
当我试图实现搜索结果的协议,我得到的编译问题:
“型SearchArticles不符合协议SearchResultsProtocol”
class SearchArticles: SearchResultsProtocol {
typealias T = ArticleProtocol
}
据我所知,它发生是因为SearchArticles类中的T不是来自具体类型(该示例中的结构),而是来自协议类型。
有没有办法解决这个问题?
提前致谢!
可能的复制[在Swift中无法使用协议作为其他协议中的关联类型](http://stackoverflow.com/questions/37360114/unable-to-use-protocol-as-associatedtype-in-another-protocol-in-swift) – Hamish