我已经在XCode中创建了一个基于命令行的项目.如果我放一个表达式println(“Hello,World!”);在一个新的swift文件中说test.swift,我会收到错误信息:表达式不允许在顶层
但是,这个表达式被放置在由新项目中的XCode创建的main.swift中的顶层.没有这样的例外被XCode标记.
解决方法
更新
这在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”.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。