PublisherとSubscriber
Combineの基本的なデータフローを理解します。
「PublisherとSubscriber」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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. 🎉
よくある質問
「PublisherとSubscriber」レッスンは無料ですか?
はい。「PublisherとSubscriber」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「PublisherとSubscriber」で何を学びますか?
Combineの基本的なデータフローを理解します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「PublisherとSubscriber」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- PublisherとSubscriber
- mapとfilterで変換する
- ライブ検索のデバウンス
- CombineをSwiftUIにつなぐ