การแปลงด้วย map และ filter
ปรับรูปแบบค่าเมื่อไหลลงไปยังปลายทาง
การแปลงด้วย map และ filter เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Operators Reshape Streams
Between a publisher and its subscriber you can insert operators that transform values as they flow downstream.
map Transforms Each Value
The map operator runs a closure on every value and emits the result, changing the stream's shape.
[1, 2, 3].publisher
.map { $0 * 10 }map Can Change the Type
Because map returns whatever you want, the Output type can change, like turning numbers into strings.
[1, 2].publisher
.map { "Item \($0)" }filter Drops Values
The filter operator keeps only the values whose closure returns true and silently discards the rest.
[1, 2, 3, 4].publisher
.filter { $0 % 2 == 0 }Chaining Operators
Operators compose cleanly: chain filter then map to first narrow the stream, then reshape it.
.filter { $0 > 0 }
.map { $0 * 2 }Order Matters
Each operator sees only the output of the one above it, so reordering map and filter can quietly change your result.
compactMap Removes nil
Use compactMap when your transform may return nil, and Combine drops those empty results for you.
["1", "x", "3"].publisher
.compactMap { Int($0) }removeDuplicates
The removeDuplicates operator emits a value only when it differs from the previous one.
subject.removeDuplicates()scan Accumulates
The scan operator carries a running result, emitting an updated total for every value, like a live sum.
.scan(0) { total, next in total + next }Readable Pipelines
Reading top to bottom, a pipeline tells a story: this is where the declarative power of operators shines.
.filter { !$0.isEmpty }
.map { $0.uppercased() }Pure Transforms
Keep operator closures pure: avoid side effects so each value transforms predictably and tests stay simple.
Quick Check
A stream emits 1, 2, 3, 4. After .filter { $0 % 2 == 0 }.map { $0 * 10 }, what is emitted?
Recap: Shaping the Flow
You learned to reshape streams: map transforms, filter narrows, and compactMap clears out nil values. ✨
เรียนรู้ Swift ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “การแปลงด้วย map และ filter” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การแปลงด้วย map และ filter” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การแปลงด้วย map และ filter”
ปรับรูปแบบค่าเมื่อไหลลงไปยังปลายทาง คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การแปลงด้วย map และ filter” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ผู้เผยแพร่และผู้รับสมาชิก
- การแปลงด้วย map และ filter
- Debounce สำหรับการค้นหาแบบทันที
- เชื่อม Combine เข้ากับ SwiftUI