Expression of type 'UIViewController?' is unused warning when pop view controller in swift 3.0

Poping view controller from navigation stack does not returns anything. Then also when I have to tried pop view controller, then it gives me warning like :


@IBAction func goBackToIntroView(_ sender: UIButton) {
     
        self.navigationController?.popViewController(animated: true)
    }


>> Expression of type 'UIViewController?' is unused


It is working fine in swift 2.x, So my question is how to pop view controller in swift 3.0 ? Is there any different method to pop view controller or developer have to store return value in any variable for posing view controller ?

If anyone know, then please suggest me.

Answered by vimipatel in 185852022

popViewController(animated:)
returns
UIViewController?
, and the compiler is giving that warning since you aren't capturing the value. The solution is to assign it to an underscore:


_ = navigationController?.popViewController(animated: true)


Before Swift 3, all methods had a "discardable result" by default. No warning would occur when you did not capture what the method returned.


In order to tell the compiler that the result should be captured, you had to add

@warn_unused_result
before the method declaration. It would be used for methods that have a mutable form (ex.
sort
and
sortInPlace
). You would add
@warn_unused_result(mutable_variant="mutableMethodHere")
to tell the compiler of it.


However, with Swift 3, the behavior is flipped. All methods now warn that the return value is not captured. If you want to tell the compiler that the warning isn't necessary, you add

@discardableResult
before the method declaration.


If you don't want to use the return value, you have to explicitly tell the compiler by assigning it to an underscore:

_ = someMethodThatReturnsSomething()


For moew clarification, please read : http://stackoverflow.com/questions/37843049/xcode-8-swift-3-expression-of-type-uiviewcontroller-is-unused-warning

Accepted Answer

popViewController(animated:)
returns
UIViewController?
, and the compiler is giving that warning since you aren't capturing the value. The solution is to assign it to an underscore:


_ = navigationController?.popViewController(animated: true)


Before Swift 3, all methods had a "discardable result" by default. No warning would occur when you did not capture what the method returned.


In order to tell the compiler that the result should be captured, you had to add

@warn_unused_result
before the method declaration. It would be used for methods that have a mutable form (ex.
sort
and
sortInPlace
). You would add
@warn_unused_result(mutable_variant="mutableMethodHere")
to tell the compiler of it.


However, with Swift 3, the behavior is flipped. All methods now warn that the return value is not captured. If you want to tell the compiler that the warning isn't necessary, you add

@discardableResult
before the method declaration.


If you don't want to use the return value, you have to explicitly tell the compiler by assigning it to an underscore:

_ = someMethodThatReturnsSomething()


For moew clarification, please read : http://stackoverflow.com/questions/37843049/xcode-8-swift-3-expression-of-type-uiviewcontroller-is-unused-warning

Expression of type 'UIViewController?' is unused warning when pop view controller in swift 3.0
 
 
Q