I am fairly new with swift and I can't understand the following situation. I am trying to extends the UIViewController class with with a couple of UITextFieldDelegate functions...
class ViewController: UIViewController{
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
In another file, if I define the function with the parameter unwrapped, the function is not called...
extension UIViewController: UITextFieldDelegate{
internal func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("Text Field Should Begin Editing called")
return true
}
but if I unwrap the parameter, it WORKS.
internal func textFieldShouldBeginEditing(_ textField: UITextField!) -> Bool
can you please help me to understand why? thank you
swift 4.2
Because Objective-C does not make any guarantees that an object is non-nil, Swift makes all classes in argument types and return types optional in imported Objective-C APIs. Before you use an Objective-C object, you should check to ensure that it is not missing.
If you use _ textField: UITextField
the parameter can be null, hence the method is not invoked by the delegate as its looking for a non null value within method signature i.e internal func textFieldShouldBeginEditing(_ textField: UITextField!) -> Bool