【SwiftUI】computed property vs func - computed property を上手に使いたい

なんとなく使っていた computed property と func。

気にはなっていたので。

以下の条件に当てはまる場合は、functionよりpropertyを使う方がよい。

- 例外を投げない
- 計算量が少ない(または初回実行時にキャッシュされる)。
- オブジェクトの状態が変化していない場合、何度呼び出しても同じ結果を返す。

(O(1)ではないcomputed propertyではその旨をドキュメント(コメント)に明記すること。 プロパティアクセスは一般的に計算コストが安価だと見なされるので、そうでない場合はその旨の明記が必要である。)

👉 [Swift] FunctionとComputed Propertyの使い分けの基準 #Swift - Qiita hatena-bookmark

* Top Hightlight
A property expresses an inherent quality of an instance, while a method performs an action.

プロパティはインスタンスの固有の品質を表現し、メソッドはアクションを実行します。

👉 Functions vs Computed property — What to use? | by Aaina jain | Swift India | Medium hatena-bookmark

うむむ、具体的にどう考えたらいいのか。

Apple 公式サンプルをさらう。


struct BackyardsSearchSuggestions: View {
  @Query private var backyards: [Backyard]
    
  var events: [BackyardVisitorEvent] {
    Set(backyards.compactMap(\.currentVisitorEvent))
      .sorted { ($0.backyard?.name ?? "") < ($1.backyard?.name ?? "") }
      .sorted { ($0.bird?.speciesName ?? "") < ($1.bird?.speciesName ?? "") }
    }
    
  var body: some View {
    ForEach(events) { event in

👉 sample-backyard-birds/Multiplatform/Backyards/BackyardsSearchSuggestions.swift at main · apple/sample-backyard-birds hatena-bookmark


struct BirdFoodPickerSheet: View {
  var backyard: Backyard
    
  @Query(sort: [.init(\BirdFood.priority, order: .reverse), .init(\BirdFood.name, comparator: .localizedStandard)])
  private var birdFood: [BirdFood]
    
  @Environment(\.dismiss) private var dismiss
  @State private var presentingBirdFoodShop = false
    
  private let metrics = BirdFoodStoreMetrics.birdFoodStore

  var premiumFood: [BirdFood] {
    birdFood.filter(\.isPremium)
  }
    
  var otherFood: [BirdFood] {
    birdFood.filter { !$0.isPremium }
  }

👉 sample-backyard-birds/Multiplatform/Backyards/BirdFoodPickerSheet.swift at main · apple/sample-backyard-birds hatena-bookmark


struct BirdsSearchSuggestions: View {
  @Query private var birds: [Bird]
    
  var speciesNames: [String] {
    Set(birds.map(\.speciesName)).sorted()
  }
    
  var body: some View {
    ForEach(speciesNames, id: \.self) { speciesName in

👉 sample-backyard-birds/Multiplatform/Birds/BirdsSearchSuggestions.swift at main · apple/sample-backyard-birds hatena-bookmark


struct PlantsSearchSuggestions: View {
  @Query private var plants: [Plant]
    
  var speciesNames: [String] {
    Set(plants.map(\.speciesName)).sorted()
  }
    
  var body: some View {
    ForEach(speciesNames, id: \.self) { speciesName in

👉 sample-backyard-birds/Multiplatform/Plants/PlantsSearchSuggestions.swift at main · apple/sample-backyard-birds hatena-bookmark


struct BackyardBirdsPassShop: View {
  @Environment(\.dismiss) private var dismiss
  @Environment(\.passIDs.group) private var passGroupID
  @Environment(\.passStatus) private var passStatus
    
  private var showPremiumUpgrade: Bool {
    passStatus == .individual || passStatus == .family
  }

👉 sample-backyard-birds/Multiplatform/Shop/BackyardBirdsPassShop.swift at main · apple/sample-backyard-birds hatena-bookmark


struct BackyardsSearchSuggestions: View {
  @Query private var backyards: [Backyard]
    
  var events: [BackyardVisitorEvent] {
    Set(backyards.compactMap(\.currentVisitorEvent))
      .sorted { ($0.backyard?.name ?? "") < ($1.backyard?.name ?? "") }
      .sorted { ($0.bird?.speciesName ?? "") < ($1.bird?.speciesName ?? "") }
  }
    
  var body: some View {
    ForEach(events) { event in

👉 sample-backyard-birds/Multiplatform/Backyards/BackyardsSearchSuggestions.swift at main · apple/sample-backyard-birds hatena-bookmark

View 内の利用状況だけをみると、

ほぼ @Query の加工にしか使ってない。

逆に言えば

@Query の加工には Computed Property が使える。

ということは言えそう。

ちなみに、View 内に func はほとんど見当たらない。


【 Swift 】2次元配列と1次元配列の相互変換 🤔

こんな12年前の so の書き込み。


p.x = index / 3;
p.y = index % 3;

👉 c# - Convert 1D array index to 2D array index - Stack Overflow hatena-bookmark

さすが、先人先生。

やってみました。

長ったらしいですが考え方を思い出したいときのために。

 

🤔 配列をn個ずつに分割する

副産物としてこんなかんじに書けました。


extension Array {
  func chunked(by: Int) -> [[Element]] {
    return  (0 ..< (self.count / by)).map {
      Array(self[$0 * by ..< ($0 + 1) * by])
    }
  }
}

print(
  [1, 2, 3, 4, 5, 6].chunked(by: 3)
)
// [[1, 2, 3], [4, 5, 6]]

 

🤔 まとめ

配列の変換


let d2: [[String]] = [
  ["あ", "い", "う", "え", "お"],
  ["か", "き", "く", "け", "こ"],
  ["さ", "し", "す", "せ", "そ"]
]

// 2次元から1次元 *
let d1 = d2.flatMap { $0 }
print(d1)
// ["あ", "い", "う", "え", "お", "か", "き", "く", "け", "こ", "さ", "し", "す", "せ", "そ"]

print(
  // 1次元から2次元 *
  (0 ..< d1.count / numCols).map {
    Array(d1[($0 * numCols) ..< ($0 + 1) * numCols])
  }
)
// [["あ", "い", "う", "え", "お"], ["か", "き", "く", "け", "こ"], ["さ", "し", "す", "せ", "そ"]]

extension 化する。


extension Array where Element: Collection { 
  func toD1() -> [Element.Element] { // nested
    return self.flatMap { $0 } 
  }
}

extension Array {
  func toD2(numCols: Int) -> [[Element]] {
    return (0 ..< self.count / numCols).map {
      Array(self[($0 * numCols) ..< ($0 + 1) * numCols])
    }
  }
}

1次元配列インデックスと2次元配列座標の関係


index = y * numCols + x

x = index % numCols
y = index / numCols

SwiftData の inMemory で実装してみたが遅かったので、@Observable クラスへ。

👉 【 SwiftUI 】 Pong Wars を SwiftUI に移植してみた hatena-bookmark

しかし、正直、勉強すればするほど謎が増えます !


👉 【Swift】2次元配列 で 転置行列 ( transpose matrix ) hatena-bookmark




Xcode の自動ビルドのせいで編集できないのだが - How to disable Automatically Refresh Canvas and background build compile

Xcode の自動ビルドというかコンパイルというか。

手動にしたいんですけど。

自動で裏でなんかしら動いて、

エディターを触るのに詰まる感じ。

ロジックをいじるときには非常にストレス。

 

😩 Settings → General → Show live issues OFF

良くわからない。

ON でも OFFでも何がどう変わるのか。

ググるとやたらヒットする。

👉 xcode 9 how to disable auto build … | Apple Developer Forums hatena-bookmark

 

😩 Editor → Canvas → Automatically Refresh Canvas OFF

You can turn off the auto-compile feature for previews/canvas'. When having a SwiftUI View open in the editor go to the Editor -> Canvas -> Automatically Refresh Canvas.

👉 Stop auto compile on Xcode preview - Stack Overflow hatena-bookmark

欲しかったのはこれかな?

ファイルを編集すると、Canvas 上に


「Preview paused 🔃」

と表示される。

🔃 を押さないと、自動でビルドは行われない。

これだわ !

 

😩 まとめ

Xcode が裏で動いてコード編集できないときは、


Editor

  ↓

Canvas 

  ↓

Automatically Refresh Canvas OFF

です。

リフレッシュするときのショートカットは、


⌥ (Option) + ⌘ (Command) + P

のようですです。

しかし、そもそも、

編集操作を妨げてまで

デフォルトで自動でリフレッシュする必要なくね?

初心者はつらい。いちいち詰まる。


【Swift】2次元配列 で 転置行列 ( transpose matrix )

これやりたくなるときありますよね。

転置行列(てんちぎょうれつ、英: transpose [of a matrix], transposed matrix)とは、m 行 n 列の行列 A に対して A の (i, j) 要素と (j, i) 要素を入れ替えてできる n 行 m 列の行列のことである。転置行列は tA, AT, A⊤, Atr, A′ などと示される。行列の転置行列を与える操作のことを転置(てんち、英: transpose)といい、「A を転置する」などと表現する

👉 転置行列 - Wikipedia hatena-bookmark

これを、変換すると、


[
  [1, 2, 3, 4, 5], 
  ["A", "B", "C", "D", "E"],
  ["あ", "い", "う", "え", "お"],
  ["か", "き", "く", "け", "こ"]
]

こうなるやつ。


[
  [1, "A", "あ", "か"],
  [2, "B", "い", "き"],
  [3, "C", "う", "く"],
  [4, "D", "え", "け"],
  [5, "E", "お", "こ"]
]

Swift で、extension で、やってみます。

 

🔄 配列系のプロトコルは多すぎないか

Xcode の反応をみながら、とりあえずいけた。

縦横インデックスを

きれいに .indecies で取りたかったけど、

うまく取れなかった。


extension Collection where Element: Collection,
                           Self.Index == Int, Element.Index == Int {

  func transposed1() -> [[Element.Element]] {
    let cols = 0 ..< (self.first?.count ?? 0)
    let rows = 0 ..< self.count
    var result: [[Element.Element]] = []
    for col in cols {
      var newRow: [Element.Element] = []
      for row in rows {
        newRow.append(self[row][col])
      }
      result.append(newRow)
    }
    return result
  }

}

// [[1, "A", "あ", "か"], [2, "B", "い", "き"], [3, "C", "う", "く"], [4, "D", "え", "け"], [5, "E", "お", "こ"]]

Array とか Collection。

where 句 や Element。

書きながらでないと、きっと理解できない感じがする。

👉 Collection | Apple Developer Documentation hatena-bookmark

 

🔄 for ループ を map に

「空配列を作成して要素追加」てのがなんとなくだるいので、

map を使います。

入れ子なので「$0」は使いません。


func transposed2() -> [[Element.Element]] {
  let cols = 0 ..< (self.first?.count ?? 0)
  let rows = 0 ..< self.count
  return cols.map { col in
    rows.map { row in
      self[row][col]
    }
  }
}

すぐに、return から始めたいので、

最初の let を省略。


func transposed3() -> [[Element.Element]] {
  return (0 ..< (first?.count ?? 0)).map { col in
    (0 ..< count).map { row in
      self[row][col]
    }
  }
}

ここまででいいか。

 

🔄 まとめ

まとめておきます。

また、勉強したら更新します。

Swift の「プロトコル」ってなんか高級。

👉 あなたの知らないCollectionの世界 #Swift - Qiita hatena-bookmark


【SwiftUI】 iOS / macOS の レイアウト記述を typealias で切り替える

よくある入力欄を iOS で作ります。


// iOS

Form {
  Section(header: Text("Name of Living Accommodation")) {
    Group {
      TextField("Enter place name here…", text: $placeName)
    }
  }
  Section(header: Text("Address of Living Accommodation")) {
    Group {
      TextField("Enter address here…", text: $address)
    }
  }
}

いい感じです。

👉 Form | Apple Developer Documentation hatena-bookmark
👉 Group | Apple Developer Documentation hatena-bookmark

macOS に切り替えてみます。

なんか気持ちが悪いです。

以下のようにレイアウト記述を書き換えます。


 iOS   | macOS
-------+----------
 Form  → List
 Group → GroupBox


// macOS

List { // *
  Section(header: Text("Name of Living Accommodation")) {
    GroupBox { // *
      TextField("Enter place name here…", text: $placeName)
    }
  }
  Section(header: Text("Address of Living Accommodation")) {
    GroupBox { // *
      TextField("Enter address here…", text: $address)
    }
  }
}

いい感じになりました。

👉 List | Apple Developer Documentation hatena-bookmark
👉 GroupBox | Apple Developer Documentation hatena-bookmark

自動で切り替えるようにしておきます。


// iOS and macOS

#if os(iOS)

Form {
  Section(header: Text("Name of Living Accommodation")) {
    Group {
      TextField("Enter place name here…", text: $placeName)
    }
  }
  Section(header: Text("Address of Living Accommodation")) {
    Group {
      TextField("Enter address here…", text: $address)
    }
  }
}

#else

List { 
  Section(header: Text("Name of Living Accommodation")) {
    GroupBox { 
      TextField("Enter place name here…", text: $placeName)
    }
  }
  Section(header: Text("Address of Living Accommodation")) {
    GroupBox { 
      TextField("Enter address here…", text: $address)
    }
  }
}

#endif

長ったらしいですね。

たった3か所の置き換えだけなのに。

■ typealias を使う

レイアウト記述の同名のエイリアスを作ってそれらを OS で切り分けます。


// iOS and macOS

#if os(iOS)
typealias TripForm = Form
typealias TripGroupBox = Group
#else
typealias TripForm = List
typealias TripGroupBox = GroupBox
#endif


 typealias    | iOS   | macOS
--------------+-------+----------
 TripForm     | Form  | List
 TripGroupBox | Group | GroupBox

それらエイリアスを使って本体は記述する。


// iOS and macOS

TripForm { // *
  Section(header: Text("Name of Living Accommodation")) {
    TripGroupBox { // *
      TextField("Enter place name here…", text: $placeName)
    }
  }
  Section(header: Text("Address of Living Accommodation")) {
    TripGroupBox { // *
      TextField("Enter address here…", text: $address)
    }
  }
}

これできれいに切り替えできました !

typealias を切り替えることで、

本体コードの挙動を置き換えてます。

さすが、Apple 公式サンプルコードは勉強になります。


Trip/Trips-SwiftData/Trips/EditLivingAccommodationsView.swift
Trip/Trips-SwiftData/Trips/SwiftUIHelper.swift

以下、Apple ページ「Download」からどうぞ。


👉 Adopting SwiftData for a Core Data app | Apple Developer Documentation hatena-bookmark