@discardableResult Swift 5

Divesh Singh
1 min readSep 16, 2019

--

Silence warning with @discardableResult

If you are not taking return value from any function, complier will give warning. Example with Warning:

Example with @discardableResult:

There are two ways to silence the warning:

use @discardableResult above the function

• Explicitly discard the return value using _

Note: you have seen warning many times while you use popViewController, it always gives warning because it’s Returns the popped controller and you should use that, if not then you have to silent warning with explicitly discard (_ = ).

_ = navigationController?.popViewController(animated: true)

  1. If you are expecting result most of the time use _ to silence the warning instead.

Don’t use @discardableResult if your expecting result most of the time.

--

--

Divesh Singh
Divesh Singh

Written by Divesh Singh

Postgraduate from JEC Jabalpur || iOS and macOS developer, traveler |||| EM at Darwinbox | Ex-Samsung || Ex - Optimize IT Sys | Ex - R Systems

No responses yet