Expected declaration

Hello, my code is this and I can't find any reason why there is a "expected declaration" error:

// Check for empty fields
if(userEmail.isEmpty || userPassword.isEmpty || userRepeatPassword.isEmpty)
{

// Display Alert Message

displayMyAlertDamage("All fields are required");

return:

}

Thank you in advance.

More than likely because you have written return:, thus indicating something should follow the colon.

return, or any other line of code, does not require a semi-colon (which is what I am guessing you intended to type) to terminate it

1 Like

As @Joanna_Carter is a typo you have after return. Also, remember, Swifty style suggest not to use semicolons (;) after each statement, they are not needed.

No actually the expected declaration is on the line:

if(userEmail.isEmpty || userPassword.isEmpty || userRepeatPassword.isEmpty)
Sorry to not have pointed that out earlier.

This compiles without error. Could you provide a complete example in the form of an entire function?

func test() {
    let userEmail = ""
    let userPassword = ""
    let userRepeatPassword = ""
    
    // Check for empty fields
    if userEmail.isEmpty || userPassword.isEmpty || userRepeatPassword.isEmpty
    {
        // Display Alert Message
        print("All fields are required")
        return
    }
}
1 Like

Thank you hexdreamer I wrote your function and it worked, not really sure why but it did,
Thank you.

Like we said, it worked because there was no colon after the return. There's no mystery or magic.

1 Like

No when I got rid of the colon there was still the error Joanna.

But you never told us exactly where in your code the error was flagged. As hexdreamer says, the code that you posted compiles fine. The only fault in it was the colon.

Yeah I agree it was a fault.

This looks like a case of a bad diagnostic. A label can be followed by a statement or a declaration, not just a declaration. If you want, you can file a bug -- it would be a good starter bug for someone learning the parser and diagnostics.

What does it mean to file a bug, how do do it?

This error appears if you use parenthesis around your entire if condition (which your sample code does). Swift wants them to be written without the parens - unlike C/Objective-C/others. IMO this is a very bad diagnostic and I've run into it myself when I forget which language I'm using or copy-paste something from another language.

The reason @hexdreamer's code didn't generate the same message is because his version omitted those parentheses.

Edit: Hmm. Maybe this depends on the version of Swift. I know I've seen this exact error and it had to do with parens in an if. I just fixed it and moved on at the time. Testing in 4.2 doesn't seem to generate it, though.

1 Like

What does it mean to file a bug, how do do it?

There’s two ways to file bugs:

In this case you should use the latter.

Finally, it’s a good idea to post the bug number after you file it; that makes it easier for folks — including, possibly, Future You™ — to track it down.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like