Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 : Warning "Unused result of call" when overriding BecomeFirstResponder

Tags:

ios

swift3

In a UIView subclass, I'm override BecomeFirstResponder (which returns a Bool.

class MyViewSubclass:UIView {
 ...
    override func becomeFirstResponder() -> Bool {
        // some stuff
        return super.becomeFirstResponder()
    }
 ...
}

When calling this method on a object of the subclass, I get a warning Result of call to 'becomeFirstResponder()' is Unused.

This message doesn't not happened when I don't override becomeFirstResponder.

I know that with Swift 3 the behaviour of methods returning results has evolved (see @discardableResult on SE-0047) but I would expect an override to have the same behaviour as its super method.

Am I wrong for thinking that or is it maybe a compiler bug ?

Edit:
FYI, I filed a bug for this.

like image 240
Matthieu Riegler Avatar asked Jul 28 '16 13:07

Matthieu Riegler


2 Answers

I am not sure if it is bug. If you have override a function, then it could totally be possible that the return value now cannot be ignored. I would simply add @discardableResult if that's what you want:

@discardableResult override open func becomeFirstResponder() -> Bool {
    // some stuff
    return super.becomeFirstResponder()
}
like image 156
Yuchen Avatar answered Oct 21 '22 17:10

Yuchen


This may be the right way, even tho im not expert on swift, but from Objective-C

class MyViewSubclass:UIView {
 ...
    override func becomeFirstResponder() -> Bool {
        // some stuff
        super.becomeFirstResponder()
     return true
    }
 ...
}
like image 26
Aragunz Avatar answered Oct 21 '22 16:10

Aragunz