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

前端之家收集整理的这篇文章主要介绍了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”.

原文链接:https://www.f2er.com/iOS/337529.html

猜你在找的iOS相关文章