ส่วนประกอบของไฟล์แอป SwiftUI
ทำความเข้าใจ @main, App, WindowGroup และ ContentView
ส่วนประกอบของไฟล์แอป SwiftUI เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The App File
Every SwiftUI project has a file named after your app, like MyFirstApp.swift. This app file is the true entry point where your whole app begins. 📱
The @main Marker
The @main attribute tells Swift exactly where your program starts. iOS launches your app by running the type marked with it.
@main
struct MyFirstApp: App {
}Conforming to App
Your main struct conforms to the App protocol. This is the SwiftUI blueprint for an application, describing what scenes it shows.
The body Property
Inside the App struct, a body property describes your app's scenes. SwiftUI reads it to know what to display when the app launches.
var body: some Scene {
WindowGroup {
ContentView()
}
}Scenes, Not Views
The app's body returns a Scene, not a View. A scene is a container for a window or screen that holds your actual views inside it.
WindowGroup
WindowGroup is the most common scene. It manages your app's main window and, on iPhone, simply fills the whole screen.
Your Root View
Inside WindowGroup you place your root view, usually ContentView(). It is the first screen the user sees when the app opens.
ContentView Lives Apart
ContentView sits in its own file, separate from the app file. Keeping the entry point and the UI apart keeps your project tidy and clear.
How It Flows
The chain is simple: @main starts the App, the App shows a WindowGroup, and the WindowGroup displays your ContentView. ✨
One @main Only
An app can have just one @main entry point. The template already provides it, so you rarely add or change this part yourself.
Where You Work
You will spend most of your time editing views like ContentView, not the app file. The app file mostly just wires everything together.
Quick Check
What is the job of the WindowGroup scene in the app file?
Recap
You learned the chain: @main on the App struct returns a WindowGroup scene that shows ContentView. That is how a SwiftUI app boots up. 🧩
เรียนรู้ Swift ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “ส่วนประกอบของไฟล์แอป SwiftUI” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ส่วนประกอบของไฟล์แอป SwiftUI” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ส่วนประกอบของไฟล์แอป SwiftUI”
ทำความเข้าใจ @main, App, WindowGroup และ ContentView คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ส่วนประกอบของไฟล์แอป SwiftUI” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การติดตั้ง Xcode และสร้างโครงการ
- ส่วนประกอบของไฟล์แอป SwiftUI
- ผืนผ้าใบแสดงตัวอย่างแบบสด
- การเรียกใช้บนตัวจำลองและอุปกรณ์