Bird
Raised Fist0
Swiftprogramming~10 mins

Main entry point and @main attribute in Swift - Step-by-Step Execution

Choose your learning style10 modes available
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.
Execution Sample
Swift
import Foundation

@main
struct MyApp {
    static func main() {
        print("Hello, Swift!")
    }
}
This program uses @main on MyApp struct; Swift calls MyApp.main() to print a greeting.
Execution Table
StepActionEvaluationResult
1Program startsFind @main typeMyApp struct found
2Call MyApp.main()Execute main() methodPrints "Hello, Swift!"
3main() completesNo more codeProgram ends
💡 Program ends after main() finishes execution
Variable Tracker
VariableStartAfter Step 2Final
Output"""Hello, Swift!\n""Hello, Swift!\n"
Key Moments - 3 Insights
Why does Swift look for a static main() method inside the @main type?
Because the @main attribute tells Swift this is the program's entry point, so it calls the static main() method to start execution (see execution_table step 2).
Can we have multiple @main types in one Swift program?
No, only one @main type is allowed because Swift needs a single entry point to start the program (execution_table step 1).
What happens if main() does not exist inside the @main type?
The program will not compile because Swift requires a static main() method in the @main type to know where to start (refer to concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed during step 2?
A"Program starts"
B"Hello, Swift!"
C"MyApp struct found"
D"Program ends"
💡 Hint
Check the 'Result' column in execution_table row for step 2.
At which step does the program find the @main type?
AStep 3
BStep 2
CStep 1
DProgram never finds it
💡 Hint
Look at the 'Action' and 'Evaluation' columns in execution_table step 1.
If the static main() method was missing, what would happen?
ACompilation error occurs
BProgram prints nothing but runs
CProgram runs normally
DProgram ends immediately
💡 Hint
Refer to key_moments about missing main() method.
Concept Snapshot
@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.
Full Transcript
In Swift, the program starts by looking for a type marked with @main. This attribute tells Swift where the program's entry point is. Swift then calls the static main() method inside that type. The main() method contains the code that runs first. For example, if main() prints "Hello, Swift!", that message appears when the program runs. The program ends when main() finishes. Only one @main type is allowed, and it must have a static main() method. If main() is missing, the program will not compile.

Practice

(1/5)
1. What does the @main attribute do in a Swift program?
easy
A. It marks the program's starting point where execution begins.
B. It defines a class as a data model.
C. It imports external libraries into the program.
D. It declares a variable as constant.

Solution

  1. Step 1: Understand the role of @main

    The @main attribute tells Swift which type contains the program's entry point.
  2. Step 2: Identify the program start

    This attribute marks where the program starts running, so it controls the first code executed.
  3. Final Answer:

    It marks the program's starting point where execution begins. -> Option A
  4. Quick Check:

    @main marks start point [OK]
Hint: Remember: @main means 'start here' in Swift programs [OK]
Common Mistakes:
  • Confusing @main with import statements
  • Thinking @main defines variables
  • Assuming @main is for classes only
2. Which of the following is the correct way to declare the main entry point using @main in Swift?
easy
A. @main struct App { static func main() { print("Hello") } }
B. struct App { @main func main() { print("Hello") } }
C. @main func main() { print("Hello") }
D. func main() { @main print("Hello") }

Solution

  1. Step 1: Check the placement of @main

    The @main attribute must be placed before a type (struct, class, or enum), not before a function.
  2. Step 2: Verify the static main() method

    The type marked with @main must have a static main() method as the entry point.
  3. Final Answer:

    @main struct App { static func main() { print("Hello") } } -> Option A
  4. Quick Check:

    @main before type + static main() [OK]
Hint: Put @main before a type, not a function [OK]
Common Mistakes:
  • Placing @main before a function instead of a type
  • Missing static keyword on main()
  • Using @main inside a function body
3. What will be the output of this Swift program?
@main struct MyApp {
    static func main() {
        print("Start")
        run()
    }
    static func run() {
        print("Running")
    }
}
medium
A. No output, program error
B. Running\nStart
C. Start
D. Start\nRunning

Solution

  1. Step 1: Identify the program start

    The @main attribute marks MyApp as the entry point, so main() runs first.
  2. Step 2: Trace the function calls

    main() prints "Start" then calls run(), which prints "Running".
  3. Final Answer:

    Start Running -> Option D
  4. Quick Check:

    main() prints Start then run() prints Running [OK]
Hint: Follow the call order inside static main() [OK]
Common Mistakes:
  • Assuming run() runs before main()
  • Forgetting to call run() inside main()
  • Thinking @main runs run() automatically
4. Identify the error in this Swift program using @main:
@main class App {
    func main() {
        print("Hello")
    }
}
medium
A. The @main attribute cannot be used on a class.
B. The main() method must be static.
C. The print statement is invalid inside main().
D. The class must be a struct instead.

Solution

  1. Step 1: Check the main() method signature

    The @main type must have a static main() method to serve as the entry point.
  2. Step 2: Identify the missing static keyword

    Here, main() is an instance method, missing static, causing a compile error.
  3. Final Answer:

    The main() method must be static. -> Option B
  4. Quick Check:

    main() must be static in @main type [OK]
Hint: Static main() is required inside @main type [OK]
Common Mistakes:
  • Omitting static keyword on main()
  • Thinking @main can't be on classes
  • Confusing instance and static methods
5. You want to create a Swift program that prints "Welcome!" only if a condition is true at startup. Using @main, which is the best way to implement this?
hard
A. struct App { static func main() { if true { print("Welcome!") } } }
B. @main struct App { func main() { if true { print("Welcome!") } } }
C. @main struct App { static func main() { if true { print("Welcome!") } } }
D. @main func main() { if true { print("Welcome!") } }

Solution

  1. Step 1: Use @main on a type with static main()

    The program must have a type marked with @main and a static main() method to run at startup.
  2. Step 2: Implement conditional print inside static main()

    Inside main(), use the if statement to print "Welcome!" only if the condition is true.
  3. Final Answer:

    @main struct App { static func main() { if true { print("Welcome!") } } } -> Option C
  4. Quick Check:

    @main type + static main() + condition inside main() [OK]
Hint: Always put @main on type with static main() method [OK]
Common Mistakes:
  • Making main() an instance method
  • Forgetting @main attribute on the type
  • Trying to put @main on a function
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard