【SwiftUI + SwiftData】@Query とは? - MV (Model + View) Pattern

C も P も VM もないんですね!

【SwiftUI + SwiftData】@Query とは? - MV Pattern ( Model + View )

「@Query」を基本的に、どう理解しておくか。


import SwiftUI

// Model
struct Product {

  static var all: [Product] { get async }
}

// View
struct ProductList: View {
  @State private var products: [Product] = []

  var body: some View {
    VStack {

    }
    .task {
      products = await Product.all
    }
  }
}

SwiftData を使うと以下のように書ける。


import SwiftUI
import SwiftData

// Model
struct Product {

}

// View
struct ProductList: View {
  @Query private var products: [Product]

  var body: some View {
    VStack {

    }
  }
}

「モデルデータコレクションの非同期取得」 ですね。

@State と同様に変更を監視しています。

👉 Stop using MVVM for SwiftUI | Apple Developer Forums hatena-bookmark


【SwiftUI】再描画の伝播 - @State と @Binding

なんとく「対」で使う雰囲気だけで使っていた @State と @Binding。

そうでもないらしい。

 

🔄 @Binding はいらない

親のカウンターの値を子に渡す。


struct Parent: View {
  // Left side of mutating operator isn't mutable: 'self' is immutable
  // private var count = 0
  @State private var count = 0

  var body: some View {
    VStack {
      Button("\(count)") {
        count += 1
      }
      Child(count: count)
    }
    .padding()
    .background(.yellow)
  }
}

struct Child: View {
  let count: Int

  var body: some View {
    Text("\(count)")
    .padding()
    .background(.white)
  }
}

どうやら、子で @Binding を付けずに受け取っても再描画される。

@State がなくても伝わる? と思ったが不可。

struct 内のプロパティは var でも immutable

らしい。

 

🔄 @Binding を付ける

親に「$」、子に「@Binding」を付けて渡す。


struct Parent: View {
  @State private var count = 0

  var body: some View {
    VStack {
      Button("\(count)") {
        count += 1
      }
      Child(count: $count) // *
    }
    .padding()
    .background(.yellow)
  }
}

struct Child: View {
  @Binding var count: Int // *

  var body: some View {
    Text("\(count)")
    .padding()
    .background(.white)
  }
}

挙動は、最初と同じ。

あれ、@Binding って何だったの?

 

🔄 子で値を更新する

今度は、子でも受け取った値を更新してみます。


struct Parent: View {
  @State private var count = 0

  var body: some View {
    VStack {
      Button("\(count)") {
        count += 1
      }
      Child(count: $count)
    }
    .padding()
    .background(.yellow)
  }
}

struct Child: View {
  @Binding var count: Int

  var body: some View {
    Button("\(count)") {
      count -= 1 // *
    }
    .padding()
    .background(.white)
  }
}

親も更新されます。

双方向に再描画できるようです。

Jetpack Compose では「単方向」のみですが、その前の Android View でいうところの「View Binding」に似ています。

 

🔄 まとめ

  • struct 内のプロパティは var でも変更できないので @State を付ける。
  • その値の変化は子に再描画を @Binding なしで伝播できる。
  • 子で @Binding で受ければ、さらに親にも伝播できる。

参照してるイメージというべきか。

👉 【SwiftUI】@State を 子 View でどう受けるか 🤔 - @Binding hatena-bookmark


【Swift】Optional 型を使っても「nil」を書きたくない 🤔

みんな大嫌いな「nil」。

できればコード内に「nil」と書きたくありません。

見るのも嫌ですね!

あんまり高度な記述もアレなので、

初心者らしく調べてみました。

 

🤔 サンプルコード

スタートはこんなかんじです。


let age: Int? = nil

if age != nil {
  if age >= 18 {
    print("成人")
  }
}

エラーです。

 

🤔 OK な記述

以下、すべて等価。


if age != nil && age! >= 18 {
  print("成人")
}


if age != nil {
  if age! >= 18 {
    print("成人")
  }
}


if let age = age {
  if age >= 18 {
    print("成人")
  }
}

👉 【Swift初心者のための】オプショナル型と if let は何のためにあるの? #Swift - Qiita hatena-bookmark


if let _ = age {
  if age! >= 18 {
    print("成人")
  }
}


if let age {
  if age >= 18 {
    print("成人")
  }
}


if let age, age >= 18 {
  print("成人")
}

 

🤔 まとめ

nil でないことを確認してそのまま使いたい場合、


if let age {
  if age >= 18 {
    print("成人")
  }
}


if let age, age >= 18 {
  print("成人")
}

の記述は、覚えやすいし、便利に使えそう。

以下の、Apple 公式のサンプルコードが調べるきっかけになりました。

👉 sample-backyard-birds/BackyardBirdsData/Birds/Bird.swift at 1843d5655bf884b501e2889ad9862ec58978fdbe · apple/sample-backyard-birds hatena-bookmark