38

Is there anything special with a main.swift file?

I have created a command line based project in XCode. If I put an expression println("Hello, World!"); in a new swift file says test.swift, I will get the error message: Expressions are not allowed at the top level

However this expression is placed at top level in the main.swift that is created by XCode in the new project. No such exception is flagged by XCode.

Anthony Kong
  • 37,791
  • 46
  • 172
  • 304
  • what is the command that you are using to run the file? – drewag Jul 08 '14 at 23:49
  • Just run it directly from within Xcode (the play button) – Anthony Kong Jul 09 '14 at 00:34
  • And the error message is flagged by the XCode IDE even before I tried to run it – Anthony Kong Jul 09 '14 at 00:35
  • Oh you can't do that. You are trying to run a full fledged program that expects a main method. Instead you can run it from the command line – drewag Jul 09 '14 at 00:48
  • Or you can put the code you want to run inside the main function – drewag Jul 09 '14 at 00:48
  • possible duplicate of [what is the entry point of swift code execution?](http://stackoverflow.com/questions/24105690/what-is-the-entry-point-of-swift-code-execution) – mmmmmm Aug 01 '14 at 17:49
  • For swift files in the new Swift Package Manager, only the main.swift file can contain instructions/expressions, and if initialised as library the package might not have a main.swift file. Launching a file in terminal with the swift command is always fine. It is different for Xcode projects using the Apple framework though, which don't have a main.swift file. – multitudes May 24 '20 at 20:04

3 Answers3

30

Apparently yes, as per this answer. However, there are no citations as to this behaviour.

Update This is documented on the Swift blog:

... earlier we said top-level code isn’t allowed in most of your app’s source files. The exception is a special file named “main.swift”, which behaves much like a playground file, but is built with your app’s source code. The “main.swift” file can contain top-level code, and the order-dependent rules apply as well. In effect, the first line of code to run in “main.swift” is implicitly defined as the main entrypoint for the program. This allows the minimal Swift program to be a single line — as long as that line is in “main.swift”.

Community
  • 1
  • 1
Manav
  • 10,094
  • 6
  • 44
  • 51
4

I wonder if it is really main.swift or perhaps you have two files. Here is a simple demo.

Folder: swift-testy
Files1: main.swift
Files2: ex1.swift

Contents are:

main.swift

import Foundation

println("Hello, World!")

let chaulky = Dog()

chaulky.bark()

ex1.swift

import Foundation

class Dog {
    func bark() {
        println("woof") // This is a comment
    }
}

Output when I click the run button

Hello, World! woof

netskink
  • 4,033
  • 2
  • 34
  • 46
0

If you have multiple swift files and you are building a command line project then make sure you have main.swift available which will act as entry point to your application. Also, main.swift should be inside your module else you will get following error:

error: the package has an unsupported layout, unexpected source file(s) found: /Users/.../../Hello/main.swift

fix: move the file(s) inside a module

manismku
  • 2,160
  • 14
  • 24