我尝试为特定结构创建协议扩展。
import UIKit
struct Vas {
let title: String
let price: Int
let idVas: String
let descriptionVas: String
let type: String
}
protocol VasCell {
var title: String {get}
}
extension VasCell where Self: Vas {
var title: String {
return "text"
}
}
这段代码完全破坏了编译器。我做错了什么?
答案 0 :(得分:1)
我解决了这个问题。在这种情况下,'Vas'也必须是协议。
extension FirstProtocol where Self: SecondProtocol {}
答案 1 :(得分:-1)
struct Vas {
let title: String
let price: Int
let idVas: String
let descriptionVas: String
let type: String
}
protocol VasCell {
typealias T
var title: String {get}
}
extension VasCell where T == Vas {
var title: String {
return "text"
}
}
struct S: VasCell {
typealias T = Vas
}
let s = S()
print(s.title) // text
struct S2: VasCell {
typealias T = Int
var title: String
}
let s2 = S2(title: "title")
print(s2.title) // title