0Pricing
SwiftUI Academy · レッスン

読み込み中とエラーの状態

スピナーを表示し、失敗に適切に対処します。

「読み込み中とエラーの状態」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Every Fetch Has Phases

A real request moves through states: loading, success, and failure. Showing each one makes your app feel trustworthy. 🔄

Modeling State with an Enum

An enum captures each phase cleanly. The view then switches on it to decide exactly what to display.

enum Phase { case loading, loaded([User]), failed(String) }

Showing a Spinner

While loading, show a ProgressView. This native spinner reassures users that something is happening behind the scenes.

ProgressView("Loading...")

Switching on the Phase

Use a switch in your body to render a spinner, the data, or an error view based on the current phase.

switch phase {
case .loading: ProgressView()
case .failed(let msg): Text(msg)
case .loaded(let users): List(users)
}

Catching the Error

Wrap the fetch in do/catch and move to the failed phase on error, storing a friendly message for the screen.

catch { phase = .failed("Could not load") }

Friendly Error Messages

Avoid raw error text. Show a clear sentence the user understands, like "Check your connection and try again."

Offering a Retry

On failure, add a retry button that runs the fetch again. Letting users recover beats a dead-end error screen.

Button("Retry") { Task { await load() } }

The Empty State

Success with no data deserves its own empty view, like "No results yet", so the screen never looks broken.

Setting Loading First

Move to the loading phase before awaiting. That instantly shows the spinner so the screen never feels frozen.

phase = .loading
phase = .loaded(try await loadUsers())

Avoiding Flicker

For very fast responses, a tiny delay before hiding the spinner prevents a jarring flash of loading UI.

Wiring It to .task

Kick off the whole flow from .task, which sets loading, fetches, and lands on loaded or failed automatically.

.task { await load() }

Quick Check

Quick check on handling request phases.

Recap: States

You now model fetches as loading, loaded, and failed, show a spinner and friendly errors with retry, and drive it all from .task. 🎯

よくある質問

「読み込み中とエラーの状態」レッスンは無料ですか?

はい。「読み込み中とエラーの状態」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。

「読み込み中とエラーの状態」で何を学びますか?

スピナーを表示し、失敗に適切に対処します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

SwiftUI Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「読み込み中とエラーの状態」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSwiftUI Academyレッスンでコードを書いて実行できますか?

はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. CodableでJSONをデコードする
  2. URLSessionでAPIを呼び出す
  3. .taskモディファイア
  4. 読み込み中とエラーの状態
← SwiftUI Academyに戻る