【SwiftUI】今どきの データモデル (Model data) のマクロ記述 📝

ネットで参考になりそうなコード記述を探すと、

新旧入り乱れてる感じがしたので、

まず読んでおいたほうがいいような気がした。

👉 Platforms State of the Union (ASL) - WWDC23 - Videos - Apple Developer hatena-bookmark

公式ドキュメントにもしっかり説明があるようなので、ざっくり古いかもしれない記述を整理しておく。


Managing user interface state | Apple Developer Documentation
👉 https://developer.apple.com/documentation/swiftui/managing-user-interface-state

Managing model data in your app | Apple Developer Documentation
👉 https://developer.apple.com/documentation/swiftui/managing-model-data-in-your-app

Migrating from the Observable Object protocol to the Observable macro | Apple Developer Documentation
👉 https://developer.apple.com/documentation/swiftui/migrating-from-the-observable-object-protocol-to-the-observable-macro

 

📝 まとめ


: ObservableObject   →   @Observable
@Published           →   不要
@StateObject         →   @State
.environmentObject() →   .environment()
@EnvironmentObject   →   @Environment
@ObservedObject      →   不要 or @Bindable

→ 「@Published*Object があれば古い。」

SwiftData を使うにしても、まずは SwiftUI のみの記述の変遷の知識もいるよな ?

先人たちの作ったネットリソースや AI系コードサジェスチョンを効率的に利用するためにも。

次は SwiftData です。