SwiftUI Academy · 课时

发布者与订阅者

理解 Combine 的核心流程。

第 1 / 4 课13 个步骤

发布者与订阅者 是 CoddyKit 上的免费 SwiftUI Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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].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. 🎉

免费开始

用 AI 导师学习 Swift — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
30
课程
120

常见问题解答

「发布者与订阅者」课时是免费的吗?

是的 — 「发布者与订阅者」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SwiftUI Academy 课程的其余内容,请升级到 CoddyKit PRO。 SwiftUI Academy 课程共包含 4 节课。

「发布者与订阅者」这节课中我会学到什么?

理解 Combine 的核心流程。 你通过在浏览器中直接运行的动手代码来练习 SwiftUI Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 SwiftUI Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 SwiftUI Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「发布者与订阅者」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 SwiftUI Academy 课中编写并运行代码吗?

能。每节 SwiftUI Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 发布者与订阅者
  2. 使用 map 与 filter 进行转换
  3. 使用防抖实现实时搜索
  4. 将 Combine 接入 SwiftUI
← 返回 SwiftUI Academy