In-depth analysis of Swift program entry and swift entry

Source: Internet
Author: User

In-depth analysis of Swift program entry and swift entry
1. Why does swift not need main?


There is a main function in c/c ++ and other languages. The program starts to execute the program from the main as the starting point, as follows:

int main(int argc, const char * argv[]) {    printf("Hello, World!\n");    return 0;}

The main function is actually a special function. In order to find the program entry, most of the languages in the building agree that the main () function is used as the entry. So why is there no such function in Swift? Let's take a look at the official explanation.

Code written at global scope is used as the entry point for the program, so you don’t need a main function. 
In general, the code in main. swift is in the global scope and serves as the entry to the entire project, so the main function is not required here.


2. Comparison between C and Swift
In ios/osx systems, the C/Objective-c project also has the main () function Convention, where the main () code block at the project entry is broken.




We can see that the ** start () ** method is called before the program is executed, and then the agreed ** main () ** method is executed, what happens in swift?




The execution sequence of Swift is start ()-> main ()-> top_level_code (). Compared with the C Project, top_level_code () is exclusive. the (non-declarative) code in swift is directly executed as the top_level_code () code. Note that the Swift language does not require entry functions. The program entry is designated as non-declaration code in main. swift. During the compilation process, the ios/osx entry adopts the agreed main () function. to be compatible with the previous entry method, the Swift language program is processed as the implicit entry function top_level_code () during compilation, and then called by main.


3. Code top_level_code ()
In the official explanation
> Code written at global scope is used as the entry point for the program, so you don't need a main function.

When it comes to the global scope, the declaration (such as variables, constants, classes, struct, and enumeration) class code in main. swift serves as the global scope and can be used by any party in the program. The non-declaration (Value assignment, for loop, if statement, etc.) code is executed as the code in top_level_code. Note that the declared class code and the non-declared class code have different scopes. The scope of the declared class is global, rather than the top_level_code () scope of the declared class code.

Note that only the code in main. swift can be executed as top_level_code.
In other files, statements of non-declarative classes cannot be directly contained in the files, but only codes of declarative classes can be contained.

4. Steal the day, replace the implicit entry function top_level_code ()
During the compilation process, the compiler. swift does not declare code to be executed as top_level_code (). In order to allow the program to execute our entry function, rather than main. swift code. You need to declare and implement this special top_level_code () function.
void top_level_code();
In the next step, let the Compilation Program specify the top_level_code () We have written. Note that

* The main. swift file cannot be deleted. If the program to be deleted cannot be compiled successfully
* To allow the Compilation Program to recognize the top_level_code () function we wrote, We need to actively use top_level_code () in the main. swift file ()
Here, two cases are used:
1. directly call the top_level_code () function in main. swift.
2. Call the top_level_code () function in methods such as the Declaration class and struct of main. swift.

Note:

Both methods will stop executing all the code in main. swift. The purpose of adding the above Code in main. swift is to let the compiler call the top_level_code function we wrote.

You can download the project directly for research.
Used in the test
1. How the Swift project calls C and Objective-c code
2. Objective-c code calls Swift code
3. Class Declaration


5. Swift print entry parameters

The ** main ** function in C language has two parameters.
* Argc: Number of strings in the command line
* Argv: pointer array pointing to a string
The two parameters are declared as global variables in Swift, respectively
 
* C_ARGC
* C_ARGV
 
You can also print the entry parameters in Swift. Note the type conversion between C and Swift.
// Convert the C language int type to Intlet cout = Int (C_ARGC) println ("all-> \ (cout)") in Swift )") let end = cout-1for index in 0... (end) {// obtain the specified C String, and convert the C String to the String type of Swift let str = String. fromCString (C_ARGV [index]) println ("\ (str )")}

After you click "run", drag the file in the Products directory of the project to the terminal, and add a space "aaa" space "bbb" to the terminal. The format is similar to the following:
/Users/mac/Library/Developer/Xcode/DerivedData/ILHelloWorld-fvywvzypiomcffbiuxdxwwdaeued/Build/Products/Debug/ILHelloWorld "aaa" "bbb"
Print the running result as follows:
all->3Optional("/Users/mac/Library/Developer/Xcode/DerivedData/ILHelloWorld-fvywvzypiomcffbiuxdxwwdaeued/Build/Products/Debug/ILHelloWorld")Optional("aaa")Optional("bbb")

The first parameter is the default program path, and the second and third parameters are the ** aaa ** and ** bbb ** we entered above. A total of three parameters are added.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.