LoginSignup
1
1

More than 1 year has passed since last update.

CoreDataのDeleteについて解きほぐす

Posted at

CoreDataテンプレートの削除部分が省略記述なので初心者にやさしくない。

何してるんだろう。

テンプレ削除部分

CoreDataテンプレートレコードの削除部分
///$0は添字の先頭
    offsets.map { items[$0] }.forEach(viewContext.delete)

削除対象の行情報を配列化してから削除する

forループなどでひとつずつ削除するとindexがズレる
上記の配列化してindexを確定した状態で削除するとズレないイメージ

省略部分を展開

$0をindex表記にする
    offsets.map { index in items[index] }.forEach(viewContext.delete)
定数に格納する
    let hoge = offsets.map { index in items[index] }
        hoge.forEach(viewContext.delete)
playgroundでmapを知る
let fruits = ["りんご","いちご","めろん"]
let offsetsArray = [1,2,0,2,0]

let newFruits = offsetsArray.map { fruits[$0] }
print("元のDB\(fruits)")
print("配列 \(newFruits)")

// 配列を1つずつ抽出
newFruits.forEach { index in
    print("=>\(index)")
}

参考

【SwiftUI】Core Dataの使い方:標準テンプレートを読み解く
Swiftのmap, filter, reduce(などなど)はこんな時に使う!
ご教示いただけますと幸いです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1