Xcode 8/Swift 3: "Expression of Type Uiviewcontroller? Is Unused" Warning

Xcode 8 / Swift 3: Expression of type UIViewController? is unused warning

TL;DR

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)

Swift 3 Change

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()

Motivation for adding this to Swift 3:

  • Prevention of possible bugs (ex. using sort thinking it modifies the collection)
  • Explicit intent of not capturing or needing to capture the result for other collaborators

The UIKit API appears to be behind on this, not adding @discardableResult for the perfectly normal (if not more common) use of popViewController(animated:) without capturing the return value.

Read More

  • SE-0047 Swift Evolution Proposal
  • Accepted proposal with revisions

Expression of type [UIViewController]? unused

Write it like this:

_ = self.navigationController?.popToViewController...

Your code works exactly the same, and the warning goes away.

Property is accessed but result is unused

You need to assign a value to the property:

loaderMonitorimi.hidesWhenStopped = true

(or false if you don't want to hide it)

If you just write loaderMonitorimi.hidesWhenStopped without any assignment, this expression will result in the boolean value stored in the hidesWhenStopped property, but the result of the expression is not used, hence the warning.

Expression resolves to an unused function (Swift)

Remove the { } phase after the in will solve the problem.
It should look like this:

RequestManager.sharedInstance.postRequest(Constants.BASE_URL + Constants.LOGIN, body: requestBody, onCompletion:  {(json: JSON) in
let result = json["response"]
print(result)
}
)

For the closure param, you should not type it by yourself to prevent typo. Use tab key to select that param, then press enter, xCode will auto generate the code for you.

If use the way I just said, the trailing closure will look like this:

RequestManager.sharedInstance.postRequest(Constants.BASE_URL + Constants.LOGIN, body: requestBody) { (json) in
let result = json["response"]
print(result)
}

After refactoring function inside closure is unused

You're currently calling that function inside of the action you pass to your showAlert function but then throwing away the action it returns. Instead, you want to pass the action that it returns directly to your showAlert method rather than wrapping it inside another action with the trailing closure syntax:

self?.showAlert(message: message, okAction: self!.startOver())

Swift 3 : Warning Unused result of call when overriding BecomeFirstResponder

The bug has been solved in the lastest Swift version.

Conditional cast from UIViewController always succeeds | Swift/Xcode

You don't need to cast it to UIViewController because properties source and destinations is UIViewController already

open var source: UIViewController { get }

open var destination: UIViewController { get }

You seeing this warning because you cast from not Optional UIViewController to optional UIViewController.

When you remove as? your code isn't running because you trying to unwrap not optional property.

Initializer for conditional binding must have Optional type, not 'UIViewController'

You should remove if do something like that:

final class FadeInPushSegue: UIStoryboardSegue {
var animated: Bool = true

override func perform() {

let sourceViewController = self.source
let destinationViewController = self.destination

let transition: CATransition = CATransition()

transition.type = CATransitionType.fade; sourceViewController.view.window?.layer.add(transition, forKey: "kCATransition")
sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false)

}


Related Topics



Leave a reply



Submit