'textFieldDidBeginEditing' nearly matches optional requirement 'textFieldDidBeginEditing' of protocol 'UITextFieldDelegate' in Swift

With "Nearly matches", the compiler tells you two things:

  1. It doesn't match: the method 'textFieldDidBeginEditing(_:)' is not the same as the delegate method 'textFieldDidBeginEditing(_:)'. This is in itself not a problem for the compiler, you just have a method with some name, and it doesn't know the name, so it's okay.
  2. It very much looks like the delegate method 'textFieldDidBeginEditing(_:)'. So the compiler can see that you probably intended this to be a delegate method, and it is telling you that, well, it did not work. What you have written is not a delegate method.

It's nice from the compiler that it tells you that there is a problem. It's not so nice that it doesn't explain what the problem is exactly.

The problem is that your class has more visibility than the delegate method. Your class is public, while your delegate method is just internal (because internal is the default, if you don't have an access specifier in your declaration.)

The fix is to give the delegate method a public access specifier too.

You have to change

func textFieldDidBeginEditing(_ textField: UITextField) {
    // ...
}

to

public func textFieldDidBeginEditing(_ textField: UITextField) {
    // ...
}

This will let the compiler know that the method is indeed intended to be a delegate method.


Bonus Content...

How did I find the solution? I reproduced the issue in Xcode. I clicked on the warning and read the Fixup: "Make 'textFieldDidBeginEditing' non-public to silence this warning". I clicked "Fix" and the line was changed to "private func textFieldDidBeginEditing(_ textField: UITextField)". So I figured that maybe turning it public instead of private would help even more. I tried it, checked it, and it worked.

Why does Swift even do this? I'm not sure, but my guess is this: if a class is public, but the protocol method is internal, it would mean that an individual view controller object implements the protocol when you look from the perspective of the module. But since the protocol implementation is internal, the protocol method would be unavailable when looking from the perspective of outside modules, e.g. from UIKit. But this is not possible in the Objective C runtime, and even if it were possible, it would be bad. Silently making the protocol methods public or semi-public would be possible, but not very clean: e.g. one could make textFieldDidBeginEditing internal, but when you cast the object to a UITextFieldDelegate pseudo-object, you can suddenly call that method, which would also be surprising.


Try this

class InterAccountTransferViewController: UIViewController,UITextFieldDelegate {
 override func viewDidLoad() {
    super.viewDidLoad()
    //Attach delegates 
    self.textfield.delegate = self
 }
   //TextField Delegate Method
   public func textFieldDidBeginEditing(_ textField: UITextField) {
   }
}