Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this overriding the method or not?

Tags:

ios

xcode8

swift3

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.

like image 635
Vemonus Avatar asked Sep 28 '16 20:09

Vemonus


People also ask

Which method is overriding?

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.

What is method overriding write an example?

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.

Which method is not overriding in Java?

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.

What does override method do?

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.


2 Answers

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 }
like image 188
rob mayoff Avatar answered Oct 13 '22 15:10

rob mayoff


Quick tutorial to find out yourself:

  • Mark / select / highlight canBecomeFirstResponder
  • Press ⌘C
  • Press ⇧⌘0 (zero not O) to get the documentation window.
  • Press ⌘V
  • Press (return)

Now you will see the declaration

var canBecomeFirstResponder: Bool { get }

Do you see the difference?

like image 44
vadian Avatar answered Oct 13 '22 15:10

vadian