替代swift中的主要功能?

时间:2023-01-23 21:37:34

I was playing around in Playground And I wondered Where is Main Function unlike in other languages , Swift does not have a main function , Whats the alternative or technique used to replace main function in swift when using in architecture outside that of Apple ( when used outside of ios and osx) since swift is soon going to be opensource ??

我在Playground玩游戏而且我想知道主要功能在哪里与其他语言不同,Swift没有主要功能,什么是替代或技术用于替换swift中的主要功能,当在Apple之外的架构中使用时(在外面使用时) ios和osx)因为swift很快将成为opensource?

1 个解决方案

#1


5  

In a playground file, execution begins at the top of the playground file.

在playground文件中,执行从playground文件的顶部开始。

If you wish, you can imagine an implicit func main() { just above the first reachable line and a } just below the last reachable line. But that's probably not constructive. Instead, it's better to just realize that not all languages need a main function, and Swift is one of those languages.

如果你愿意,你可以想象一个隐式的func main(){正好在第一个可到达的行之上,而}正好在最后一个可到达行的正下方。但这可能不具有建设性。相反,最好只是意识到并非所有语言都需要主要功能,Swift就是其中之一。

For example, if we write a shell script, it will look something like this:

例如,如果我们编写一个shell脚本,它将如下所示:

mkdir workspace
cd workspace
git clone https://github.com/nhgrif/SQLConnect

This is very simple... but there's no "main" function. Despite this, the terminal would be perfectly satisfied running this script, which would create a "workspace" directory and download my SQLConnect library into it.

这很简单......但是没有“主要”功能。尽管如此,终端完全满意地运行这个脚本,这将创建一个“工作区”目录并将我的SQLConnect库下载到其中。

A playground file is executed in much the same way as many interpreted languages. You do not need a main function. Single .swift files that are executed via the swift command in the terminal are executed in the exact same way. They start at the top of the file and work their way down.

操场文件的执行方式与许多解释语言的执行方式大致相同。您不需要主要功能。通过终端中的swift命令执行的单个.swift文件以完全相同的方式执行。它们从文件的顶部开始,然后向下移动。

#1


5  

In a playground file, execution begins at the top of the playground file.

在playground文件中,执行从playground文件的顶部开始。

If you wish, you can imagine an implicit func main() { just above the first reachable line and a } just below the last reachable line. But that's probably not constructive. Instead, it's better to just realize that not all languages need a main function, and Swift is one of those languages.

如果你愿意,你可以想象一个隐式的func main(){正好在第一个可到达的行之上,而}正好在最后一个可到达行的正下方。但这可能不具有建设性。相反,最好只是意识到并非所有语言都需要主要功能,Swift就是其中之一。

For example, if we write a shell script, it will look something like this:

例如,如果我们编写一个shell脚本,它将如下所示:

mkdir workspace
cd workspace
git clone https://github.com/nhgrif/SQLConnect

This is very simple... but there's no "main" function. Despite this, the terminal would be perfectly satisfied running this script, which would create a "workspace" directory and download my SQLConnect library into it.

这很简单......但是没有“主要”功能。尽管如此,终端完全满意地运行这个脚本,这将创建一个“工作区”目录并将我的SQLConnect库下载到其中。

A playground file is executed in much the same way as many interpreted languages. You do not need a main function. Single .swift files that are executed via the swift command in the terminal are executed in the exact same way. They start at the top of the file and work their way down.

操场文件的执行方式与许多解释语言的执行方式大致相同。您不需要主要功能。通过终端中的swift命令执行的单个.swift文件以完全相同的方式执行。它们从文件的顶部开始,然后向下移动。