xcode – 如果模块不是main.swift,则表达式不允许在顶层

有什么特别的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”.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...