Hi. This is my first day using Swift, and I'm trying to print the statement "Hello, World!", but it's not letting me do it. It says "Expressions are not allowed on the top level." By the way, I am using Xcode 12.3.
Printing Objects in Swift
Problem here is :
That is your first day, so you have a lot of excuses.
But you should paste copy of code (use Paste And Match Style) and format with the code formatter tool (<>) instead of referencing a screenshot.
Screenshots are very useful to show the result (such as a resulting display), not to show code.
Last point. If this solves your problem, don't forget to close the thread my marking the answer as correct (tap on the circled checkmark in front of the correct answer).
you declare a statement outside of any class.
you should include this print inside a func, such as the viewDidLoad func.
That is your first day, so you have a lot of excuses.
But you should paste copy of code (use Paste And Match Style) and format with the code formatter tool (<>) instead of referencing a screenshot.
Screenshots are very useful to show the result (such as a resulting display), not to show code.
Last point. If this solves your problem, don't forget to close the thread my marking the answer as correct (tap on the circled checkmark in front of the correct answer).
When you want to test a simple expression like print("Hello, World!") on the top level, it needs to be written in a fixed file main.swift, no other file names are allowed.
And main.swift would only work with the project template: macOS Command Line Tool.
Create a new project macOS Command Line Tool, and edit main.swift.
Or if you want to test your code in a GUI app, you need to move the line print("Hello, World!") into some function.
And main.swift would only work with the project template: macOS Command Line Tool.
Create a new project macOS Command Line Tool, and edit main.swift.
Or if you want to test your code in a GUI app, you need to move the line print("Hello, World!") into some function.