0Pricing
Swift Academy · レッスン

ロギング、assert/precondition/fatalError

print/debugPrint で状態を記録し、不変条件に対して assert (デバッグ時のみ)、 precondition (リリース時も検査)、 fatalError (クラッシュ)を使い分けます。

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

診断ツールボックス

簡単なロギングで状態を確認し、assert、precondition、fatalErrorで前提条件を適用します。

  • assert: デバッグ時のみ行うチェック
  • precondition: リリース時にもチェックされる
  • fatalError: 直ちに停止する

軽量なロギング

printは読みやすく、debugPrintはより詳細な情報を表示します(デバッグ中に便利です)。

let user = ["name": "Ana", "role": "admin"]
print("User:", user)            // user-friendly
debugPrint("User:", user)       // includes type details when available

let point = (x: 3, y: 5)
print("Point:", point)
debugPrint("Point:", point)

assert(デバッグ時のみ)

条件がfalseの場合、assertはDebugでプログラムを停止します。最適化されたReleaseビルドでは削除されます。

// Even numbers only (demo)
func half(of x: Int) -> Int {
    assert(x % 2 == 0, "Expected even number, got \(x)")
    return x / 2
}
print(half(of: 8))   // OK in Debug/Release
// In Debug, failing the assert would stop and print the message.
// In Release, assert checks are disabled by default.

precondition(リリース時にもチェック)

preconditionはDebugとReleaseで重要な要件を検証します。決して発生してはならないプログラマーのミスに使用してください。

// Safe divide with required precondition
func safeDivide(_ a: Int, by b: Int) -> Int {
    precondition(b != 0, "Divider must be nonzero")
    return a / b
}
print(safeDivide(10, by: 2))    // 5
// If called with 0, precondition fails in Debug and Release.

fatalError(即時停止)

fatalErrorは実行を直ちに停止し、メッセージを報告します。開発中の到達不能なコードパスや未実装の機能に使用してください。

enum FileKind { case text, binary }

func open(kind: FileKind) {
    switch kind {
    case .text:
        print("Open text mode")
    case .binary:
        print("Open binary mode")
    // If we ever add a new case and forget to handle it, we could use:
    // default: fatalError("Unsupported file kind")
    }
}
open(kind: .text)

使い分け

ガイドライン:

  • 簡単なログにはprint/debugPrintを使用します(後で削除するか、フラグで有効・無効を切り替えてください)。
  • デバッグ時のみ行う開発者向けのチェックにはassertを使用します。
  • 要件を満たさない場合に、どのビルドでもプログラムを続行してはならないときはpreconditionを使用します。
  • 到達不能または未実装のパスにはfatalErrorを使用します。

リリース時にもチェックされる診断

確認問題: Releaseビルドでもチェックされる診断はどれですか?

まとめ

まとめ: print/debugPrintでログを出力し、assert(デバッグ時)とprecondition(リリース時にも)で不変条件を保護し、本当に到達不能な状態にはfatalErrorを使用します。

よくある質問

「ロギング、assert/precondition/fatalError」レッスンは無料ですか?

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

「ロギング、assert/precondition/fatalError」で何を学びますか?

print/debugPrint で状態を記録し、不変条件に対して assert (デバッグ時のみ)、 precondition (リリース時も検査)、 fatalError (クラッシュ)を使い分けます。 ブラウザで直接実行するハンズオンコードでSwift Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「ロギング、assert/precondition/fatalError」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. XCTestの基本(ターゲット、フィクスチャ)
  2. ロギング、assert/precondition/fatalError
← Swift Academyに戻る