การออกแบบ ViewModel
สร้างโมเดลมุมมอง @Observable สำหรับหน้าจอหนึ่งหน้า
การออกแบบ ViewModel เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
A ViewModel Is Just a Class
A ViewModel is usually a class that holds the state and logic for one screen. Using a class lets several views share the same live instance.
class ProfileViewModel {
var name: String = ""
}Make It Observable
Add the @Observable macro so SwiftUI tracks property changes and re-renders the view automatically when state updates.
@Observable
class ProfileViewModel {
var name = ""
}Expose View-Ready State
Give the ViewModel properties the view can read directly, like a formatted greeting, so the view never does its own string building.
var greeting: String {
"Hi, " + name
}Hold the Loading Flag
Track UI status inside the ViewModel. A simple isLoading boolean lets the view decide when to show a spinner versus content.
var isLoading = falseKeep Intent in Methods
Expose actions as methods like load() or save(). The view calls them on tap; the ViewModel decides exactly what happens.
func load() {
isLoading = true
}Name Properties for the UI
Name state after what the screen shows, like buttonTitle or rows, not after raw data. This keeps the view almost declarative.
Hide the Messy Details
Parsing, math, and edge cases belong inside the ViewModel. The view should see clean values and never touch the rough work.
One ViewModel per Screen
Give each screen its own focused ViewModel. A small, single-purpose model is far easier to read, reason about, and test.
Start Empty, Then Fill
Initialize the ViewModel with sensible empty defaults, then populate it when data arrives. The view shows a calm empty state meanwhile.
Avoid View Code Inside
Never import view types or layout code into a ViewModel. Keeping it UI-free is what makes it portable and easy to unit test. ✨
A Complete Small Example
This tiny ViewModel exposes a count and a method to change it, giving the view everything it needs in two clear members.
@Observable
class CounterViewModel {
var count = 0
func increment() { count += 1 }
}Quick Check
Which macro makes a ViewModel class trigger SwiftUI updates?
Recap: Designing a ViewModel
You built an @Observable class that exposes view-ready state and intent methods while hiding the messy details. Next you will bind views to it.
คำถามที่พบบ่อย
บทเรียน “การออกแบบ ViewModel” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การออกแบบ ViewModel” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การออกแบบ ViewModel”
สร้างโมเดลมุมมอง @Observable สำหรับหน้าจอหนึ่งหน้า คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การออกแบบ ViewModel” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใด MVVM จึงเหมาะกับ SwiftUI
- การออกแบบ ViewModel
- เชื่อมมุมมองกับ ViewModels
- การฉีดการพึ่งพาสำหรับ ViewModels