【发布时间】:2021-09-10 04:28:50
【问题描述】:
所以我只是让我的 ViewController 符合UITextFieldDelegate。
我的理解是:
Delegate 只是一个协议。协议有一些要求,在UITextFieldDelegate 的情况下,其中一些看起来像..
protocol UITextFieldDelegate: class {
var delegate: (Not sure of this type actually) { get set }
func textFieldDidEndEditing(_ textField: UITextField)
func textFieldDidBeginEditing(_ textField: UITextField)
//etc, etc.
}
为什么我没有实现任何方法时没有编译错误?它们是不需要的,还是 UIViewController 已经隐式地符合这些方法?
【问题讨论】:
-
因为他们是
optional。见developer.apple.com/documentation/uikit/uitextfielddelegate/… 这更像是一种Objective-C 方式,因为理论上协议方法不能是可选的。见hackingwithswift.com/example-code/language/… 或stackoverflow.com/questions/24032754/…
标签: swift