التحويل باستخدام map وfilter
أعد تشكيل القيم أثناء تدفقها إلى المصب
التحويل باستخدام map وfilter درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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. ✨
الأسئلة الشائعة
هل درس «التحويل باستخدام map وfilter» مجاني؟
نعم — نص درس «التحويل باستخدام map وfilter» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «التحويل باستخدام map وfilter»؟
أعد تشكيل القيم أثناء تدفقها إلى المصب تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «التحويل باستخدام map وfilter»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الناشرون والمشتركون
- التحويل باستخدام map وfilter
- Debounce للبحث المباشر
- ربط Combine بـ SwiftUI