0Pricing
SwiftUI Academy · Lekcja

Wydawcy i subskrybenci

Poznaj podstawowy przepływ Combine

Wydawcy i subskrybenci to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej SwiftUI Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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].publisher

Output 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: Never

What 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. 🎉

Często zadawane pytania

Czy lekcja „Wydawcy i subskrybenci” jest bezpłatna?

Tak — pełny tekst „Wydawcy i subskrybenci” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu SwiftUI Academy, przejdź na CoddyKit PRO. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Wydawcy i subskrybenci”?

Poznaj podstawowy przepływ Combine Ćwiczysz SwiftUI Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć SwiftUI Academy?

Nie wymagamy żadnego doświadczenia. SwiftUI Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.

Ile czasu zajmuje lekcja „Wydawcy i subskrybenci”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji SwiftUI Academy?

Tak. Każda lekcja SwiftUI Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Wydawcy i subskrybenci
  2. Przekształcanie za pomocą map i filter
  3. Debounce dla wyszukiwania na żywo
  4. Łączenie Combine ze SwiftUI
← Powrót do SwiftUI Academy