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