質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%

Q&A

解決済

1回答

482閲覧

検索時のエンティティの記述の方法

pon3428

総合スコア14

0グッド

0クリップ

投稿2023/04/14 09:17

実現したいこと

検索時のエンティティの記述の方法を知りたいです。

前提

エンティティItemをitemsでインスタンス化しています。

発生している問題・エラーメッセージ

familyNames = items.text.filter { $0.contains(searchText) } } の所で Cannot assign value of type '[Binding<Subject.Element>]' to type '[String] Referencing subscript 'subscript(dynamicMember:)' requires wrapper 'Binding<FetchRequest<Item>.Configuration>' Value of type 'FetchedResults<Item>' has no dynamic member 'text' using key path from root type 'FetchRequest<Item>.Configuration' と出ます。 また、ContentView_PreviewsでItemへの記述を求められ item: Item とすると Cannot convert value of type 'Item.Type' to expected argument type 'Item' となります。

該当のソースコード

SwiftUI

1 2struct ContentView: View { 3 4 @Environment(\.managedObjectContext) private var viewContext 5 @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],animation: .default) 6 private var items: FetchedResults<Item> 7 @State var name = "" 8 @ObservedObject var model: Model 9 @State var list = ["default"] 10 11 var body: some View { 12 VStack{ 13 TextField("text", text: $name).padding() 14 NavigationView { 15 List { 16 Section{ 17 ForEach(items) { item in 18 NavigationLink { 19 EditView(item: item) 20 } label: { 21 Text(item.text ?? "") 22 } 23 } 24 } 25 } 26 .searchable( 27 text: $model.searchText, 28 placement: .navigationBarDrawer(displayMode: .always), 29 prompt: Text("all") 30 ) 31 .onSubmit(of: .search) { 32 model.fetchFamilyNames() 33 } 34 } 35 } 36 } 37 } 38 39func deleteItems(offsets: IndexSet) { 40 withAnimation { 41 offsets.map { items[$0] }.forEach(viewContext.delete) 42 do { 43 try viewContext.save() 44 } catch { 45 let nsError = error as NSError 46 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") 47 } 48 } 49 } 50 } 51 52class Model: ObservableObject { 53 54 @Environment(\.managedObjectContext) private var viewContext 55 @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],animation: .default) 56 private var items: FetchedResults<Item> 57 @Published var searchText = "" 58 @Published var familyNames: [String] = [] 59 60 func fetchFamilyNames() { 61 if searchText.isEmpty { 62 familyNames = list 63 } else { 64 familyNames = items.text.filter { 65 $0.contains(searchText) 66 } 67 } 68 } 69} 70

試したこと

itemsをitemとしました。

補足情報(FW/ツールのバージョンなど)

Xcode14.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

他にも修正した方が良いところがあるかもしれませんが、
items.text.filter {}についてだけ書いてみますね。

familyNamesがStringの配列で良いのでしょうか?
良いのでしたら、次のような感じになるかなと思います。

swift

1familyNames = items 2 .filter({ $0.text?.contains(searchText) ?? false }) 3 .map({ $0.text ?? "" })

投稿2023/04/14 11:18

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

pon3428

2023/04/14 12:49

ありがとうございます よく分からないことを書いてしまいました familyNamesが検索時に結果を代入する配列です。 textはアトリビュートです
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問