Swift Program Entrance Depth analysis

Source: Internet
Author: User

1.swift Why don't you need main


There is a main function in the C + + and other languages, with the program starting from main and executing the program 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 entrance to the program, most of the building language is the main () function as the entrance. So why isn't there such a function in Swift? Let's take a look at the official explanation.




In general, the code in Main.swift is under the global scope, and the code directly serves as the entry for the entire project, so the main function is not required here.


2.C and Swift's entrance comparison
In the IOS/OSX system, the C/OBJECTIVE-C project also has the Convention of the main () function, which breaks the point in the Project entry main () code block




It can be seen that the **start () * * method was called before executing the program, and then the agreed **main () * * method was implemented, which we can understand well, so what happens in Swift?




It can be seen that the order of execution of Swift is start ()->main ()->top_level_code (), with respect to the C language project, more out of Top_level_code (), The (non-declarative) code in Main.swift is executed directly as the Top_level_code () code. Note here that the swift language itself does not require an entry function, and the program entry is specified as a non-declarative code in Main.swift. At the specific compilation stage, the IOS/OSX's entrance adopts the agreed main () function, in order to be compatible with the previous entry method, the Swift language program is processed into the implicit entry function Top_level_code () and then called by Main () in the compilation process.


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


Referring to the global scope, the declaration (such as variables, constants, classes, structs, enumerations) in Main.swift Class code, as a global scope, can be used by any party in the program. The non-declaration (assignment, for loop, if statement, and so on) code is executed as code in Top_level_code (). Notice here that the Declaration class code is not the same scope as the non-declarative class code. Declaring a class is scoped to the global scope, not to the Top_level_code () scope of the declared class code.


It is particularly important to note that only code in Main.swift can be executed as Top_level_code.
In other files, it is not directly in the file containing non-declarative classes of statements, only the declaration of the class code.

4. Bogus, replacing the implicit entry function Top_level_code ()
At compile-time, the compiler executes main.swift non-declarative Code as Top_level_code (), in order to allow the program to execute our entry function instead of Main.swift code, declare and implement this special Top_level_code () function
void Top_level_code ();




The next step is how to get the compiler to specify the Top_level_code () that we wrote, which we need to note


* Main.swift file cannot be deleted if the removal program directly cannot compile through
* In order for the compiler to endorse the Top_level_code () function We wrote, we need to actively use the Top_level_code () in the Main.swift file.
The use here refers to two situations:
1. The Top_level_code () function that we write is called directly in Main.swift
2. Call the Top_level_code () function in a method of the Main.swift declaration class, struct, and so on

Attention

Both of these methods will cause all code in Main.swift to no longer execute. The purpose of adding the above code here in Main.swift is to have the compiler call the Top_level_code function we wrote.



Specifically, you can download the project directly to study
In order to test the inside.


1. How the SWIFT project calls C and Objective-c code
2. Objective-c code calls SWIFT code
3. Declarations of Classes


This part of the knowledge will be explained in the following one by one, we only need to understand the principle of the entrance.


5.Swift Printing Entry Parameters


In the **main** function in C speech, there are two parameters


* ARGC: Number of strings in the command line
* argv: Array of pointers to strings


These two parameters are declared as global variables in swift, respectively

* C_ARGC
* C_ARGV

We can also print out the entry parameters in swift, noting type conversion in C and Swift
Convert the C-language Int to Intlet cout = Int (C_ARGC) println ("All->\ (cout)") in swift and let end = Cout-1for index in 0 ... (end) {    //Gets the specified C-language string and converts the C string to Swift's string type let    str = string.fromcstring (C_argv[index])    println ("\ (str)")}





After you click Run Complete, drag the project products catalog into the terminal and add the space "AAA" Space "BBB", similar to the following format
/users/mac/library/developer/xcode/deriveddata/ilhelloworld-fvywvzypiomcffbiuxdxwwdaeued/build/products/debug/ Ilhelloworld "AAA" "BBB"



The print run results are 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, the second and third parameters are the **aaa** and **bbb** we entered above, plus a total of 3 parameters.

Swift Program Entrance Depth analysis

Related Article

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.