Expressions is not allowed on the top level

Source: Internet
Author: User

In Swift, the method inside the class is called directly outside of the class, and the bug:expressions is allowed at the top level appears.

The reasons are:

In App Engineering, . Swift files are compiled into modules and cannot have top level code.

First clear a concept, a. Swift file execution begins with its first non-declarative statement (expression, control structure) and includes the assignment portion of the declaration (corresponding to the MOV instruction or the LEA Directive), all of which constitute the top_level_code of the. Swift file () function. All declarations, including structs, classes, enumerations, and their methods, are not part of the Top_level_code () code, where the code logic is contained in other regions, and Top_level_code () can call them directly. The entrance to the program is an implied main (argc, argv) function, which performs the logic of setting the global variable C_ARGC c_argv, and then calls Top_level_code (). Not all. Swift files are available as modules, and for now, any. Swift files that contain expression statements and control statements are not allowed as modules . Normally a module can contain global variables (VAR), Global constants (Let), structs (structs), Classes (Class), enumerations (enums), protocols (protocol), Extensions (extension), Functions (func), and Global properties (Var { Get set}). The overall point here is defined at the top level. The expression referred to here refers to expressions, the statement refers to statement, and the Declaration refers to Declaration. Therefore, if a method inside the class is called directly outside the class in your code, the. Swift file is not compiled, and the error is compiled.

Expressions is not allowed on the top level

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.