We all know that in the Swift language which is no longer the main function,You may understand C language or Java language students arrived at this point of deep discomfort。In short,Instead main.swift。
1 2 3 4 |
int main(int argc, const charchar * argv[]) { printf("Hello, World!\n"); return 0; } |
Swift language in which,The compiler does not go looking for main function as a program entry,But this "main.swift" file,According to the official explanation view,The contents of the document is under global scope,It will serve as the project's entrance,And other documents which are not directly written statements in addition to statements。
1 2 3 |
import Foundation print("Hello World!",appendNewline true) |
The main function disappeared
Then,Without this function,We get the parameters of the entrance from where? Argc and argv are familiar with where to go?
In fact, these two parameters Swift is still there - after all, Swift compatible C is not it?
We just need to extract them from the global variables which can be:
1 2 3 |
//在 Swift 中,这二者分别被声明为如下形式 Process.arguments |
Get parameters
To obtain parameters,We just want to extract them among the global variable - Type is a String array:
1 2 3 4 5 6 7 |
let argv = Process.arguments for arg in argv { //打印所有接收到的参数 print(arg) } |
Such,We will be able to get the passed parameters,of course,The default is still the first program in the current directory。
Original article written by LogStudio:R0uter's Blog » Swift as how to receive the inlet parameters like C language?
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1029.html