I recently was forced to update to Swift 3.0 and it seems like Xcode's conversion did not do a great job. Luckily, I am able to solve the majority of the issues manually, however, one has me a bit confused.
This line that overrode the canBecomeFirstResponder
method in uiController worked prior to upgrading to Swift 3.0:
override func canBecomeFirstResponder() -> Bool {
return true
}
However, it now returns the error:
Method does not override any method from its superclass.
However, removing the override
bit doesn't seem to fix it, as it raises a different error that seems to contradict the previous one:
Method 'canBecomeFirstResponder()' with Objective-C selector 'canBecomeFirstResponder' conflicts with getter for 'canBecomeFirstResponder' from superclass 'UIResponder' with the same Objective-C selector
Is there actually an override occurring? Why is Xcode giving me mixed messages here?
For reference, I have looked at this question and although it is very similar, it is about a class, and this is about a method. To be safe, I did try using the solution to that question and it further broke things and raised the same errors.
In Java, method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes.
Example of method overriding In this example, we have defined the run method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method are the same, and there is IS-A relationship between the classes, so there is method overriding.
Static methods can not be overridden We can not override the static methods in a derived class because static methods are linked with the class, not with the object. It means when we call a static method then JVM does not pass this reference to it as it does for all non-static methods.
The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides.
canBecomeFirstResponder
was changed from a method in Swift 2.2 to a property in Swift 3.0. This means that you must change your code to override it as a property instead of a method.
override var canBecomeFirstResponder: Bool { return true }
Quick tutorial to find out yourself:
canBecomeFirstResponder
Now you will see the declaration
var canBecomeFirstResponder: Bool { get }
Do you see the difference?
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