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