Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Swift will not let a function or closure return a value it never declared

ErrorUnexpected non-void return value in void function

Short answer

Swift reads a function or closure with no return type as returning Void, the empty tuple. When your code writes return with a value inside it, the compiler rejects it. Either add the return type to the declaration, or remove the value because the closure you are inside is declared to return nothing.

  • Applies to Swift 6, Xcode 26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Click the red error and look at the nearest opening brace above the return line. That brace belongs to the function or closure that Swift thinks returns nothing. Ask whether it is the function you meant, or a closure inside it.

    // Search the file for the return statements near the error line
    grep -n "return " MyViewController.swift
  2. 2Match what you see, then jump to its fix

    1. 1The error points at a return line directly inside a func whose signature ends at the closing parenthesisThe function declaration has no return typeFix
    2. 2The enclosing func does have a return type, but the return line is inside a { } block passed to another function, such as a completion handlerThe return sits inside a closure that is declared to return VoidFix

What does the error look like?

  • Unexpected non-void return value in void function

Cause 1 of 2

The function declaration has no return type

1Check

Read the signature. A function without an arrow and a type is a Void function, so it has nothing to hand back.

// This signature declares no return type
func loadTitle(for id: Int) {
    return "Title \(id)"
}

2Fix

Add the return type to the signature. The type goes after the arrow, and every code path must then return it.

func loadTitle(for id: Int) -> String {
    return "Title \(id)"
}

3Verify

Build again. The error disappears and callers receive the value.

let title = loadTitle(for: 3)
print(title)

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

The return sits inside a closure that is declared to return Void

1Check

A completion handler is typed like (Picture) -> Void. A return with a value inside that closure answers the closure, not the outer function, so it is rejected.

func fetchName(completion: (String) -> Void) { }

func name() -> String {
    fetchName { value in
        return value   // returns from the closure, which must give back Void
    }
    return ""
}

2Fix

Do not return from the closure. Pass the value on, by assigning it to a variable you own, by calling another handler, or by making the outer function take its own completion handler.

func name(completion: @escaping (String) -> Void) {
    fetchName { value in
        completion(value)
    }
}

3Verify

Build again, then call the outer function with a trailing closure and read the value there.

name { value in
    print(value)
}

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Write the return type first when you declare a function, then the body.
  • When a return line is flagged, check which pair of braces it really lives in before changing the signature.
  • Prefer a completion handler or async function over returning from inside another function's closure.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

InstanceID token() no longer works in a Swift Firebase app

Instance ID calls fail to compile after upgrading Firebase to 8.0.0 or later

Firebase removed the Instance ID dependency from Messaging in 8.0.0, so InstanceID.instanceID() calls no longer belong in a current app.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.