Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 208 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'Expressions are not allowed at the top level' if the module is not main.swift

#1
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.
Reply

#2
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
Reply

#3
Apparently yes, as per this [answer](

[To see links please register here]

). <strike>However, there are no citations as to this behaviour</strike>.

**Update**
This is documented on [the Swift blog](

[To see links please register here]

):

> ... 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”.
Reply

#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
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through