Swift will not let a function or closure return a value it never declared
Unexpected non-void return value in void functionShort 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.
Fix it fast
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.swift2Match what you see, then jump to its fix
- 1
The error points at a return line directly inside a func whose signature ends at the closing parenthesisThe function declaration has no return typeFix - 2
The 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
- 1
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
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
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
UserDefaults logs 'Failed to read values in CFPrefsPlistSource' for an app group
Settings saved by the app are not visible to its extension, widget or sibling app that share the group
This log comes from the preferences system when UserDefaults is opened for an app group's suite.
3 causes, each with a fixMobile apps
iOS app crashes: Google Maps SDK must be initialized via provideAPIKey
Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use
The Maps SDK throws this when a Maps object is created before GMSServices.provideAPIKey has run.
3 causes, each with a fixMobile apps
UIKit app crashes because it cannot find the storyboard
Could not find a storyboard named 'Main' in bundle NSBundle
UIStoryboard throws this exception when no storyboard file with that name exists in the bundle it searches.
3 causes, each with a fixMobile apps
Firebase on iOS asks for the FCM token before APNs has given one
Be sure to re-retrieve the FCM token once the APNS device token is set.
The Firebase Messaging SDK needs the APNs device token from iOS before it hands out an FCM token.
3 causes, each with a fixMobile apps
A test push from the Firebase console never shows up on the iPhone
didReceiveRegistrationToken prints a token, yet no notification arrives
When a console message is accepted but the iPhone stays silent, the break is between Firebase and APNs or inside the app.
4 causes, each with a fixMobile 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