Concept Flow - Main entry point and @main attribute
Program Start
@main Struct or Class
Call static main()
Execute main() code
Program Ends
Swift looks for a type marked with @main and calls its static main() method to start the program.
import Foundation @main struct MyApp { static func main() { print("Hello, Swift!") } }
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Program starts | Find @main type | MyApp struct found |
| 2 | Call MyApp.main() | Execute main() method | Prints "Hello, Swift!" |
| 3 | main() completes | No more code | Program ends |
| Variable | Start | After Step 2 | Final |
|---|---|---|---|
| Output | "" | "Hello, Swift!\n" | "Hello, Swift!\n" |
@main attribute marks the program's entry point. Swift calls the static main() method inside the @main type. Only one @main type allowed per program. main() contains code that runs first. Program ends when main() finishes.