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