ผู้เผยแพร่และผู้รับสมาชิก
ทำความเข้าใจการไหลหลักของ Combine
ผู้เผยแพร่และผู้รับสมาชิก เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Meet Combine
Combine is Apple's framework for handling values that arrive over time, like taps, timers, or network responses. 🌊
Values Over Time
Instead of asking for data once, you describe a stream of values and react each time a new one arrives, now or later.
What a Publisher Does
A Publisher is the source. It emits values downstream and tells you when it finishes or fails.
let publisher = [1, 2, 3].publisherOutput and Failure
Every publisher declares two types: its Output (the values it sends) and its Failure (the error it may throw).
Just(42) // Output: Int, Failure: NeverWhat a Subscriber Does
A Subscriber attaches to a publisher, receives each value as it arrives, and decides what to do with it.
Connecting with sink
The sink subscriber is the simplest one: you hand it a closure that runs for every value received.
publisher.sink { value in
print(value)
}Handling Completion
A fuller sink takes two closures: one for completion (finished or failure) and one for each value.
publisher.sink(
receiveCompletion: { print($0) },
receiveValue: { print($0) }
)Storing the Subscription
Subscribing returns a cancellable. Keep it alive, or the stream is torn down immediately.
var bag = Set<AnyCancellable>()
publisher.sink { _ in }
.store(in: &bag)Just and Empty
Just emits one value then finishes. Empty finishes with no values at all. Both are handy building blocks.
let one = Just("hello")Subjects Push Values
A PassthroughSubject lets you send values manually, perfect for forwarding events you control.
let subject = PassthroughSubject<Int, Never>()
subject.send(5)The Pull and Push Flow
The subscriber signals demand, the publisher pushes values, and completion ends the relationship cleanly.
Quick Check
You attached a closure to a publisher and nothing printed. What likely went wrong?
Recap: The Combine Trio
You met the three players: a publisher emits values, a subscriber receives them, and a stored cancellable keeps the link alive. 🎉
เรียนรู้ Swift ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “ผู้เผยแพร่และผู้รับสมาชิก” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ผู้เผยแพร่และผู้รับสมาชิก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ผู้เผยแพร่และผู้รับสมาชิก”
ทำความเข้าใจการไหลหลักของ Combine คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “ผู้เผยแพร่และผู้รับสมาชิก” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ผู้เผยแพร่และผู้รับสมาชิก
- การแปลงด้วย map และ filter
- Debounce สำหรับการค้นหาแบบทันที
- เชื่อม Combine เข้ากับ SwiftUI