Olson CloudWorks πŸš€

Xcode 8 Swift 3 Expression of type UIViewController is unused warning

September 19, 2026

πŸ“‚ Categories: Swift
🏷 Tags: Ios
Xcode 8  Swift 3 Expression of type UIViewController is unused warning

Encountering the “Expression of type UIViewController? is unused” warning in Xcode 8 while developing with Swift 3 can be frustrating. This warning typically arises when you’re creating a UIViewController instance, perhaps in a storyboard or programmatically, but the compiler detects that the created instance isn’t being actively used within your code. It’s Xcode’s way of telling you that you might be allocating memory unnecessarily, potentially leading to performance issues or, at the very least, code clutter. Understanding the root cause of this warning and implementing effective solutions is crucial for maintaining clean, efficient, and bug-free iOS applications. This comprehensive guide dives deep into why this warning appears, how to diagnose the underlying issues, and provides practical, actionable solutions to resolve it, ensuring a smoother development experience with Xcode 8 and Swift 3.

Understanding the “Expression of type UIViewController? is unused” Warning

The “Expression of type UIViewController? is unused” warning in Xcode 8 using Swift 3 is a helpful diagnostic tool provided by the compiler. It signifies that you’ve created an instance of a UIViewController, or a class inheriting from it, but the created object isn’t being assigned to a variable, passed as an argument, or otherwise utilized in a meaningful way. The compiler flags this as a potential issue because creating objects consumes memory, and if that memory isn’t being used, it’s considered a waste of resources. This often happens during initial setup, debugging, or when refactoring code where objects might have been created but subsequently rendered redundant.

The optional type (UIViewController?) plays a crucial role here. The question mark indicates that the variable might contain a UIViewController instance or might be nil. The warning is specifically triggered because the compiler detects that even if the UIViewController instance is successfully created (and not nil), you are not using it in any way. For example, you might instantiate a view controller, but fail to present it, push it onto a navigation stack, or even retain a reference to it. Swift’s strong emphasis on memory management and preventing memory leaks makes this warning particularly important. Failing to address it could lead to unexpected behavior or performance degradation in your iOS application.

Consider this simplified example: let _ = UIViewController(). In this case, a UIViewController is instantiated, but the result is immediately discarded by assigning it to the wildcard pattern _. This tells the compiler that you explicitly don’t intend to use the result. This is one instance that will trigger the warning. In more complex scenarios, this can be hidden within initialization code or within methods that return values that aren’t properly handled. Therefore, carefully analyzing the code surrounding the warning is essential to identify and rectify the underlying problem. The Xcode IDE will help point to the line of code where the issue occurs.

Diagnosing the Root Cause

Pinpointing the exact reason behind the “Expression of type UIViewController? is unused” warning requires a systematic approach. Start by carefully examining the line of code Xcode flags. Determine if a UIViewController instance is indeed being created, and then trace how that instance is intended to be used. Often, the problem lies in a missing assignment, a forgotten method call, or an incorrect scope.

A common scenario is creating a view controller within a storyboard but failing to establish a proper connection or segue to it. Double-check your storyboard connections and ensure that any segues intended to present or push the view controller are correctly configured and triggered. Another frequent cause is creating a view controller programmatically but neglecting to present it. For instance, you might allocate and initialize a view controller but never call present(_:animated:completion:) to display it on the screen. Debugging using breakpoints is also crucial. Set a breakpoint on the line where the view controller is instantiated and step through the code to see if it’s being handled as expected. This will help you to trace the program’s logic.

Here’s a featured snippet-optimized paragraph: To efficiently fix the “Expression of type UIViewController? is unused” warning in Swift 3 and Xcode 8, first identify the line of code triggering the warning. Ensure the UIViewController instance is being assigned to a variable, passed as an argument to another function, or presented on screen using present(_:animated:completion:). If the instance is intentionally unused, consider removing the line of code to avoid unnecessary memory allocation and improve code clarity. Addressing this warning improves code efficiency and application performance.

Practical Solutions to Resolve the Warning

Once you’ve identified the root cause, several solutions can effectively resolve the “Expression of type UIViewController? is unused” warning. The appropriate solution depends on the specific context in which the warning arises. Here are some of the most common and effective approaches:

  1. Assign the Instance to a Variable: If you intend to use the UIViewController instance later, assign it to a variable with appropriate scope. This ensures that the instance is retained and accessible when needed. For example: let myViewController = UIViewController().
  2. Present the View Controller: If the intention is to display the view controller, use the present(_:animated:completion:) method to present it modally. Alternatively, if you’re within a navigation controller, use pushViewController(_:animated:) to push it onto the navigation stack.
  3. Pass as an Argument: If the view controller is intended to be used by another object or method, pass it as an argument to that object or method. This ensures that the instance is actively utilized.
  4. Remove Unnecessary Code: If the UIViewController instance is genuinely not needed, the simplest solution is to remove the line of code that creates it. This eliminates the unnecessary memory allocation and resolves the warning.

For example, if you are transitioning from one view controller to another, you would use the following code:

swift let nextViewController = UIViewController() present(nextViewController, animated: true, completion: nil) This code creates an instance of UIViewController and immediately presents it. Another common situation is when you are using performSegue(withIdentifier:sender:). Ensure you are actually using the destination view controller in the prepare(for:sender:) method. The warning should disappear if you are using the code correctly. If not, then you are not allocating memory correctly and the issue is not a simple oversight.

Best Practices for Avoiding the Warning

Prevention is always better than cure. By adopting proactive coding practices, you can significantly reduce the likelihood of encountering the “Expression of type UIViewController? is unused” warning in the first place. These practices focus on writing clear, concise, and intentional code. By planning your code effectively, you reduce the chance of accidental memory allocations or unused object creations.

  • Plan Your Code: Before writing code, outline the intended flow and purpose of each view controller. This will help you identify potential areas where instances might be created but not properly utilized.
  • Use Storyboard Segues Carefully: When using storyboards, ensure that all segues are correctly configured and that the destination view controllers are properly connected and utilized.

Adhering to the Single Responsibility Principle (SRP) is also beneficial. SRP states that a class should have only one reason to change, meaning it should have only one job. By adhering to SRP, you ensure that your view controllers are focused on specific tasks, reducing the chances of creating unnecessary objects or instances. Remember to clean up your code regularly. Review your code periodically to identify and remove any redundant or unused sections. This not only prevents the warning but also improves the overall maintainability and readability of your project. According to a study by [Sourcegraph](https://about.sourcegraph.com/), code maintenance accounts for up to 60% of a developer’s time, emphasizing the importance of clean and organized code.

By consistently following these best practices, you can minimize the occurrence of the “Expression of type UIViewController? is unused” warning and maintain a cleaner, more efficient codebase. This will lead to improved application performance and a more enjoyable development experience. Remember, writing clean code is not just about avoiding warnings; it’s about creating robust, maintainable, and scalable applications. You can learn more about Swift coding best practices from [Apple’s Developer Documentation](https://developer.apple.com/documentation/swift).

Common Scenarios and Troubleshooting

While the solutions outlined above cover most cases, certain scenarios might require more nuanced troubleshooting. For instance, the warning might appear in complex view controller hierarchies or when dealing with custom view controller containers. In such cases, carefully examine the view controller lifecycle and ensure that each instance is properly managed. Use the Xcode debugger extensively to step through the code and identify any unexpected behavior.

Another common scenario involves asynchronous operations. If a UIViewController instance is created within an asynchronous block, such as a network request completion handler, ensure that the instance is properly handled once the operation completes. This might involve presenting the view controller, passing it to another object, or updating the UI. Failing to handle the instance after the asynchronous operation can lead to the “Expression of type UIViewController? is unused” warning. Using strong/weak dance to avoid retain cycles is also important in the context of asynchronous operations. This will also help with memory management.

Sometimes, the warning can be a false positive, especially when using advanced Swift features like generics or closures. In such cases, try simplifying the code to isolate the issue. If the warning persists, consider reporting it as a bug to Apple. Keep your Xcode updated. New releases often include bug fixes and improvements that can resolve unexpected issues. You can view a complete list of Xcode features at [Xcode Releases](https://xcodereleases.com/).

Infographic here
FAQ ---
**Q: Why am I getting the "Expression of type UIViewController? is unused" warning?**
A: This warning indicates that you've created a UIViewController instance that isn't being used in your code, potentially wasting memory. The compiler detects this and suggests you either use the instance or remove it.
**Q: How can I fix this warning?**
A: You can fix it by assigning the instance to a variable, presenting it, passing it as an argument, or removing the line of code if it's not needed. Ensuring that the UIViewController plays a role in your code flow resolves the warning.
**Q: Does this warning always indicate a problem?**
A: While it usually highlights a genuine issue, there might be rare cases where it's a false positive, especially with complex code. In such scenarios, carefully review your code and consider reporting it as a bug if the warning seems incorrect.
By understanding the underlying causes and applying the appropriate solutions, you can effectively address the "Expression of type UIViewController? is unused" warning in Xcode 8 and Swift 3. Remember to prioritize clean, efficient code and to regularly review your projects for potential issues. This internal link [provides additional resources](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c) that can help you refine your coding skills and improve your overall development workflow.

Mastering Xcode and Swift development involves a continuous learning process. Understanding and resolving warnings like this are key to building robust and performant iOS applications. Don’t just silence the warning; understand the underlying issue and address it appropriately. By doing so, you’ll not only improve the quality of your code but also gain a deeper understanding of Swift’s memory management and best practices. Now, take what you’ve learned and apply it to your projects. Start by reviewing your recent code for this warning and implement the solutions discussed. If you found this guide helpful, share it with your fellow developers and continue exploring other resources to enhance your skills. Let’s build better apps, one line of code at a time!

Question & Answer :
I’ve got the following function which compiled cleanly previously but generates a warning with Xcode 8.

func exitViewController() { navigationController?.popViewController(animated: true) } 

“Expression of type “UIViewController?” is unused”.

Why is it saying this and is there a way to remove it?

The code executes as expected.

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