【发布时间】:2018-06-01 07:40:11
【问题描述】:
所以我有一个包含自定义视图的 viewController,
并且那个 viewController 类符合ViewProtocol
我预计当someAction 方法在someCustomizedView 中触发时
它将打印" method in otherCustomizedClass called "
但它会打印 (" method in extension Called")。
theNotOptionalMethod 工作正常,但不是可选方法。
我对协议扩展有什么误解吗?
请帮忙,已经挣扎了几个小时,谢谢
protocol ViewDelegate: class {
func theNOTOptionalMethod()
}
extension ViewDelegate {
func theOptionalMethod(){
print (" method in extension Called")
}
}
class someCustomizedView: UIView {
weak var deleage: ViewDelegate?
@IBAction func someAction(sender: UIButton) {
deleage?.theOptionalMethod()
}
}
class someCustomizedVC: UIViewController, ViewDelegate {
lazy var someView: someCustomizedView = {
var v = someCustomizedView()
v.deleage = self
return v
}()
//...... someView added to controller
func theNOTOptionalMethod() {
// do nothing
}
func theOptionalMethod() {
print (" method in otherCustomizedClass called ")
}
}
【问题讨论】:
标签: ios swift protocols swift-protocols protocol-extension