Expected declaration

Hello! I started learning swift and on the start I have a problem 😟


print("Hello, World!")


Error: Expected declaration


Can someone help?

Accepted Reply

In programming languages, the same line -- exactly the same sequence of characters can have different meanings according to the context.

So, if you get an error message at a specific line, you need to show some more context including the line to get a clue to fix it.


For example, this code snippet generates the same error message as you reported:

class MyClass {
    print("Hello, World!") //->Expected declaration
}


But this does not:

class MyClass {
    func someMethod() {
        print("Hello, World!")
    }
}

Replies

Is this in a playground or in another file?

Does Swift.print("Hello, World!") work?

In programming languages, the same line -- exactly the same sequence of characters can have different meanings according to the context.

So, if you get an error message at a specific line, you need to show some more context including the line to get a clue to fix it.


For example, this code snippet generates the same error message as you reported:

class MyClass {
    print("Hello, World!") //->Expected declaration
}


But this does not:

class MyClass {
    func someMethod() {
        print("Hello, World!")
    }
}