SwiftUI Academy · บทเรียน

จากสถานะมุมมองสู่คลาสโมเดล

แยกข้อมูลที่ใช้ร่วมกันออกมาเป็นคลาส

บทเรียน 1 จาก 413 ขั้นตอน

จากสถานะมุมมองสู่คลาสโมเดล เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Outgrowing @State

When a screen has many @State values, your view starts juggling data and UI at once. That mix gets messy fast as the screen grows. 🤔

What a Model Holds

A model is a plain object that holds your data and the rules around it, kept separate from how things look on screen.

A Simple Model Class

Start by moving related values into one class so they live together in a single, tidy place.

class Counter {
    var count = 0
}

Methods Carry the Logic

Put behaviour next to the data with methods, so the view just asks the model to act instead of doing the work itself.

class Counter {
    var count = 0
    func increment() { count += 1 }
}

Why a Class, Not a Struct

A class is a reference type, so every view sharing the same instance sees the exact same data, not its own private copy.

Views Stay Thin

With logic in the model, your view body shrinks to mostly layout. A thin view is far easier to read and change later.

One Source of Truth

Keeping data in the model gives you one source of truth, so screens never disagree about the current value.

Reusing the Model

Because the model is independent of any view, you can reuse it across several screens or even in tests.

Holding It in a View

A view can create and hold a model instance as a property, ready to read its values and call its methods.

struct CounterView: View {
    let model = Counter()
    var body: some View { Text("Count") }
}

The Missing Piece

So far the view can read the model, but it will not yet refresh when the data changes. We will fix that with observation next.

Separation of Concerns

This split is called separation of concerns: the view shows, the model decides. Each part has one clear job.

Quick Check

You moved your data into a class. Quick question about why.

Recap

You learned to lift data and logic out of a view into a model class, keeping views thin and giving your app one source of truth. ✨

เริ่มต้นได้ฟรี

เรียนรู้ Swift ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “จากสถานะมุมมองสู่คลาสโมเดล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “จากสถานะมุมมองสู่คลาสโมเดล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “จากสถานะมุมมองสู่คลาสโมเดล”

แยกข้อมูลที่ใช้ร่วมกันออกมาเป็นคลาส คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “จากสถานะมุมมองสู่คลาสโมเดล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม

ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. จากสถานะมุมมองสู่คลาสโมเดล
  2. มาโคร @Observable
  3. @State สำหรับออบเจ็กต์ที่เป็นเจ้าของ
  4. ใช้โมเดลร่วมกันผ่าน @Environment
← กลับไปที่ SwiftUI Academy