0Pricing
SwiftUI Academy · レッスン

@State プロパティの宣言

@State を追加して、ビュー内で変更可能なデータを保持します。

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

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

Meet the @State Wrapper

You declare mutable view-local data by marking a property with @State. This property wrapper tells SwiftUI to store and watch the value for you. 🏷️

@State private var count = 0

Always Give It a Default

A @State property needs an initial value right where you declare it. SwiftUI uses it to create the storage the first time the view appears.

@State private var isOn = false

Mark It private

State belongs to one view, so declare it private. This signals that no outside code should reach in and set it directly, keeping ownership clear.

@State private var name = ""

Reading the Value

Read a @State property like any normal variable. Just use its name in body and SwiftUI tracks that this view depends on it.

Text("You tapped \(count) times")

Writing the Value

You can mutate a @State property even though the view is a struct. Assign to it from an action and SwiftUI schedules a redraw automatically.

Button("Add") { count += 1 }

It Persists Across Redraws

SwiftUI keeps the @State value alive across the many times the view body is recomputed. Your count won't reset just because the view redrew.

Putting It Together

Declare state, read it in the body, and mutate it from a button. That is the full loop that powers most simple interactions.

struct CounterView: View {
    @State private var count = 0
    var body: some View {
        Button("Count \(count)") { count += 1 }
    }
}

Use Simple Value Types

@State works best with value types like Int, Bool, String, or small structs. For shared objects you'll later reach for other tools, but values are the norm here.

One Owner Per Property

The view that declares @State is its owner. Other views can receive access, but only one view should hold the original state for a given value.

Don't Overuse It

Reach for @State only when a value truly changes while the view is on screen. Constant labels and passed-in data don't need it.

Naming for Clarity

Name state for what it represents, like isLoading or selectedTab. Clear names make the data flow obvious to anyone reading the view.

Quick Check

Which line correctly declares a private piece of view-local state?

Recap

Declare changing view data with @State, give it a default, and keep it private. Read it freely in body and mutate it from actions to trigger redraws. ✅

よくある質問

「@State プロパティの宣言」レッスンは無料ですか?

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

「@State プロパティの宣言」で何を学びますか?

@State を追加して、ビュー内で変更可能なデータを保持します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「@State プロパティの宣言」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. View に State が必要な理由
  2. @State プロパティの宣言
  3. タップ カウンターの作成
  4. State による表示の切り替え
← SwiftUI Academyに戻る