协议继承和打字不理解
Protocols inheritance and typing incomprehension
我有点困惑。请看这个例子。
我创建了一个 VM 协议:
protocol VM {
}
我的 VC 实现中正在使用此协议
final class VC: UIViewController {
let viewModel: VM
}
现在我创建特殊的新协议
protocol AwesomeProtocol {
}
protocol AwesomeViewProtocol {
var viewModel: AwesomeProtocol { get }
}
我的想法是用 Awesomeness
扩展 VM
所以:
protocol VM: AwesomeProtocol {
}
final class VC: UIViewController, AwesomeViewProtocol {
let viewModel: VM
}
但是我遇到了一个编译错误:
Type 'VC' does not conform to protocol 'AwesomeViewProtocol'
尽管 VM
扩展了 AwesomeProtocol
有人可以解释我做错了什么吗?
你必须实现这个。
final class VC: UIViewController, AwesomeViewProtocol {
var viewModel: AwesomeProtocol
}
计算变量接近函数。他们的签名在父子中必须相同(继承)classes/protocols.
如果您需要抽象的东西,请改用 assosiatedtype
和通用 类。
我有点困惑。请看这个例子。
我创建了一个 VM 协议:
protocol VM {
}
我的 VC 实现中正在使用此协议
final class VC: UIViewController {
let viewModel: VM
}
现在我创建特殊的新协议
protocol AwesomeProtocol {
}
protocol AwesomeViewProtocol {
var viewModel: AwesomeProtocol { get }
}
我的想法是用 Awesomeness
扩展 VM
所以:
protocol VM: AwesomeProtocol {
}
final class VC: UIViewController, AwesomeViewProtocol {
let viewModel: VM
}
但是我遇到了一个编译错误:
Type 'VC' does not conform to protocol 'AwesomeViewProtocol'
尽管 VM
扩展了 AwesomeProtocol
有人可以解释我做错了什么吗?
你必须实现这个。
final class VC: UIViewController, AwesomeViewProtocol {
var viewModel: AwesomeProtocol
}
计算变量接近函数。他们的签名在父子中必须相同(继承)classes/protocols.
如果您需要抽象的东西,请改用 assosiatedtype
和通用 类。