Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does @unknown default (Swift 5) cause compile error?

Tags:

ios

swift

swift5

I think I understand the logic behind @unknown default, and I apologize for screenshots instead of code, but it's the only way to see the error messages in context.

Converted a project to Swift 5. It runs, but gets this warning, which I understand:

enter image description here

So I let Xcode fix it for me, and got this:

enter image description here

I changed the order of the stub cases (this is a work in progress), which makes the compiler happy again:

enter image description here

Am I doing something I shouldn't, or is this weird compiler behavior?

like image 585
Ron Avatar asked Jan 27 '23 11:01

Ron


1 Answers

The error in the middle image looks like a bug in swift and it could be solved by adding a semicolon at the end of the return statement.

In general the compiler is expecting @unknown default to be the last case. Check @unknown documentation from apple where they explain why it must be used with the last case in a switch and more on the "unknown patterns" link in the following quote:

@unknown may only be applied to default or a case consisting of the single pattern _. Even in the latter case, @unknown must be used with the last case in a switch. This restriction is discussed further in the "unknown patterns" section under "Future directions".

like image 120
denis_lor Avatar answered Feb 13 '23 07:02

denis_lor