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

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

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

总结

以上是内存溢出为你收集整理的xcode – 如果模块不是main.swift,则表达式不允许在顶层全部内容,希望文章能够帮你解决xcode – 如果模块不是main.swift,则表达式不允许在顶层所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1113339.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存