有什么特别的main.
swift文件吗?
我已经在XCode中创建了一个基于命令行的项目.如果我放一个表达式println(“Hello,World!”);在一个新的swift文件中说test.swift,我会收到错误信息:表达式不允许在顶层
但是,这个表达式被放置在由新项目中的XCode创建的main.swift中的顶层.没有这样的例外被XCode标记.
解决方法
显然是的,根据这个
answer.然而,没有引用这个行为.
更新
这在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”.