Challenge - 5 Problems
Swift Main Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a Swift program with @main attribute
What is the output of this Swift program using the @main attribute?
Swift
import Foundation @main struct HelloWorld { static func main() { print("Hello, Swift!") } }
Attempts:
2 left
💡 Hint
The @main attribute marks the program's entry point.
✗ Incorrect
The @main attribute tells Swift where the program starts. The static main() method is called automatically, printing the message.
❓ Predict Output
intermediate2:00remaining
Output when multiple @main structs exist
What happens if you compile this Swift code with two structs marked @main?
Swift
import Foundation @main struct FirstApp { static func main() { print("First") } } @main struct SecondApp { static func main() { print("Second") } }
Attempts:
2 left
💡 Hint
Swift requires exactly one @main entry point.
✗ Incorrect
Swift does not allow more than one @main attribute in a program. This causes a compilation error.
🔧 Debug
advanced2:00remaining
Why does this @main struct fail to compile?
This Swift program uses @main but fails to compile. Why?
import Foundation
@main
struct App {
func main() {
print("Hello")
}
}
Swift
import Foundation @main struct App { func main() { print("Hello") } }
Attempts:
2 left
💡 Hint
The entry point method must be static.
✗ Incorrect
The @main attribute requires a static main() method. Instance methods cannot be used as entry points.
🧠 Conceptual
advanced1:30remaining
Purpose of the @main attribute in Swift
What is the main purpose of the @main attribute in a Swift program?
Attempts:
2 left
💡 Hint
Think about where the program begins running.
✗ Incorrect
The @main attribute tells Swift which type contains the program's starting point, the main() method.
❓ Predict Output
expert2:30remaining
Output of a Swift program using @main with async main
What is the output of this Swift program using an async main method with @main?
Swift
import Foundation @main struct AsyncApp { static func main() async { try? await Task.sleep(nanoseconds: 1_000_000_000) // 1 second print("Async Hello") } }
Attempts:
2 left
💡 Hint
Swift 5.5+ supports async main methods with @main.
✗ Incorrect
Swift allows async static main() methods with @main. The program waits 1 second then prints the message.