Short answer

"Pushing a navigation controller is not supported" means you passed a UINavigationController, or a subclass of one, to pushViewController. Make the screen a plain UIViewController and push that, or present a new navigation controller if it should be a separate flow. "Unexpectedly found nil while unwrapping an Optional value" means a ! met a nil. Replace the ! with if let, guard let or ?? and a default that means something, not an empty string by reflex.

SWIFT TIPS · UIKIT · DEBUGGING

First posted on September 22, 2018 for Swift 4, as "[Swift 4] Error in Xcode Edition". Rewritten on November 18, 2026. The navigation test was run on a Mac as a Mac Catalyst program, which runs UIKit on macOS (not an iPhone or the simulator), and the Optional examples as a small macOS program, with Swift 6.2.3 (Xcode 26.2) on macOS 26.0.1. Both test programs caught the error and exited quietly instead of crashing. The view controllers were type-checked against the iOS 26.2 SDK in Swift 5 and Swift 6 modes with iOS 15 and iOS 26.2 as deployment targets, but not run on a device.

In 2018 I started a list of errors I had run into, with the fix for each. It had two: an app that crashed with "pushing a navigation controller is not supported", fixed by changing a class from UINavigationController to UIViewController, and the "found nil while unwrapping" crash, fixed by replacing ! with ?? and a default value. Both fixes are right. This rewrite shows each error as it appears today, explains why it happens, and adds the options the short version left out.

1. Pushing a navigation controller

The 2018 screen was declared as class SecondViewController: UINavigationController. I pushed that class onto a navigation controller in the Catalyst program:

Pushing a UINavigationController subclass onto a navigation controller:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'

UIKit checks what you push and stops with an exception before anything appears. Each navigation controller manages its own stack of screens and its own bar, and UIKit doesn't allow one to be pushed into another's stack. The subclass is still a navigation controller, which is why renaming it isn't enough: it has to inherit from UIViewController, as the 2018 fix did.

One easy way to get here is a storyboard: Xcode's "Embed In Navigation Controller" wraps a screen, and then code pushes the wrapper instead of the screen inside it. If the screen should open as a separate flow with its own bar, present the navigation controller instead of pushing it.

2. Unexpectedly found nil

The second error came from print(data.item!) where item was nil. Run as a small program:

data.item ?? "": ""
Force-unwrapping data.item:
o060_unwrap.swift:11: Fatal error: Unexpectedly found nil while unwrapping an Optional value

The ! says "I'm sure this has a value". When it doesn't, Swift stops the app on that line. The 2018 fix, data.item ?? "", stops the crash, but the code in the post wouldn't compile as written: the quotes were typographic curly quotes, usually a sign that the code passed through an editor that changes straight quotes into curly ones. Type-checked with Xcode 26.2:

o060_2018.swift:3:23: error: unicode curly quote found, replace with '"'
o060_2018.swift:3:26: error: unicode curly quote found, replace with '"'
o060_2018.swift:4:7: warning: expression implicitly coerced from 'String?' to 'Any'

The warning on the last line is for printing an Optional without unwrapping it: print(data.item) prints Optional("...") or nil, which is rarely what you want in a log.

The fixes in code

import UIKit

// 1. Push plain view controllers. Wrap one in a navigation controller only to present it.
final class SecondViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        title = "Second"
    }
}

final class FirstViewController: UIViewController {
    func showSecond() {
        // Inside a navigation stack: push the screen itself.
        navigationController?.pushViewController(SecondViewController(), animated: true)
    }

    func showSecondAsSheet() {
        // As a separate flow with its own bar: present a new navigation controller.
        let nav = UINavigationController(rootViewController: SecondViewController())
        present(nav, animated: true)
    }
}

// 2. Handle a missing value on purpose instead of with "!".
struct Order: Decodable {
    let id: Int
    let item: String?
    let quantity: Int?
}

func describe(_ order: Order) -> String {
    // A default that means something to the reader, not an empty string.
    let name = order.item ?? "Unnamed item"
    // Stop early when a value you need is missing.
    guard let quantity = order.quantity else {
        return "\(name): quantity unknown"
    }
    return "\(name) x\(quantity)"
}

func logItem(of order: Order) {
    // Unwrap once, then use the plain value.
    if let item = order.item {
        print("Item:", item)
    } else {
        print("Order \(order.id) has no item")
    }
}

I ran describe(_:) with three orders decoded from JSON, one complete, one with a null item and one with no quantity:

order 1: Coffee x2
order 2: Unnamed item x1
order 3: Tea: quantity unknown

Which fix to use

  • ?? with a default when there's a sensible value to show. "Unnamed item" tells the reader something is missing; an empty string hides it.
  • guard let when the rest of the function can't work without the value. It exits early and leaves the unwrapped value available below.
  • if let when you have something different to do in each case, like the two log lines in logItem(of:).
  • ! only when nil would be a programming mistake you want to find during development, never for data from an API, a user or a file.

A quick check for your project

  1. Search for pushViewController( and check that nothing passed to it is a UINavigationController or a storyboard scene embedded in one.
  2. Search for !) and !. on values that come from JSON or user input.
  3. Search for ?? "" and ?? 0. Each one should be a value you'd be happy to show the user.
  4. If you paste code from a web page and see "unicode curly quote found", retype the quotes.

What the 2018 version said

The post showed each problem as an "Error condition" and a "Fix condition", and ended with "Next Update If i Found Error in my code". The Int example used data.item ?? 0.

Sources