Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

592
Visualizações
'textFieldDidBeginEditing' nearly matches optional requirement 'textFieldDidBeginEditing' of protocol 'UITextFieldDelegate' in Swift

I am working on an app where I have created modules for each feature of the app. I have to switch from one Module UI Controller to another Module UI Controller.

I have UIController in a Module and I mark that controller as Public access identifier as below

public class InterAccountTransferViewController: UIViewController {
   override public func viewDidLoad() {
    ......
    ......
   }
}

Above class also implements UITextField delegates in extension. When I created above class as Open access I am getting a warning on TextField delegates as below

Instance method 'textFieldDidBeginEditing' nearly matches optional requirement 'textFieldDidBeginEditing' of protocol 'UITextFieldDelegate'

enter image description here

Now textfield delegates are not being called. When I tried to turn off a warning by making delegates as private, they are still not being called.

Please let me know how to silence these warnings & call the delegates as well.

Any idea or suggestion would be great. I am working on Xcode 10 with swift 4.2. Please let me know if I have to explain my question more.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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) {
   }
} 
over 4 years ago · Santiago Trujillo Relatório

0

Mark your method also as public

public func textFieldDidBeginEditing(_ textField: UITextField)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda