@Stateと@Binding
各プロパティに適した所有権を選びます。
「@Stateと@Binding」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Two Wrappers, One Goal
Both @State and @Binding manage mutable data, but they play different roles. Picking the right one keeps ownership clear. 🎯
@State Owns
Use @State when this view is the source of truth. It creates and stores the value for itself.
@State private var count = 0@Binding Borrows
Use @Binding when the value lives somewhere else and this view only needs to read and write it.
@Binding var count: IntWho Creates the Value
@State initializes a value. @Binding never does; it always receives one from a parent that owns it.
Mark State Private
@State is view-local, so mark it private. No one outside should reach in and set it directly.
@State private var isOn = falseBindings Stay Open
A @Binding is not private; the parent must be able to pass a value in when it creates the child.
The Dollar Sign Link
An owners @State turns into a child @Binding through the dollar sign. That is the bridge between the two.
ChildView(count: $count)Top of the Tree
The view highest up that truly owns the data uses @State. Everything below it borrows with @Binding.
A Simple Rule
Ask: do I create this value? If yes, @State. If a parent hands it to me, @Binding. That choice rarely fails you.
Wrong Choice, Real Bugs
Use @State where you meant @Binding and the child edits a copy. The parent never sees the change, and the UI quietly desyncs.
Same Value, Two Roles
One piece of data is @State in its owner and @Binding in every borrower. Same value, different responsibilities. 🔄
Quick Check
Quick check on choosing wrappers.
Recap: Own or Borrow
Use @State to own a value and @Binding to borrow one. Match the wrapper to who creates the data and your views stay in sync. 🧠
AI チューターと学ぶ Swift — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 30
- レッスン
- 120
よくある質問
「@Stateと@Binding」レッスンは無料ですか?
はい。「@Stateと@Binding」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「@Stateと@Binding」で何を学びますか?
各プロパティに適した所有権を選びます。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「@Stateと@Binding」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 子ビューに@Bindingが必要な理由
- バインディングを下位ビューに渡す
- 再利用可能なトグル行を作る
- @Stateと@Binding