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'
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.
With "Nearly matches", the compiler tells you two things:
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) {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With