実現したいこと
ContentView_PreviewsでのContentViewのプロパティの記述
前提
ContentViewではなく
EditViewで行っています。
EditViewのプロパティに
var item: Item
があります。
ItemはEntityです。
発生している問題・エラーメッセージ
Cannot convert value of type '(tem.Type'to expected argument type 'Item'
該当のソースコード
struct Editview_Previews: PreviewProvider { static var previews: some View { Editview(item: Item) )
試したこと
プロパティをItemではなくitemにしました。
補足情報(FW/ツールのバージョンなど)
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
下記のような質問は推奨されていません。
- 質問になっていない投稿
- スパムや攻撃的な表現を用いた投稿
適切な質問に修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。

回答1件
0
ベストアンサー
Cannot convert value of type '(tem.Type'to expected argument type 'Item'
のエラーを解消したいということですよね。
Editview_PreviewsからItemエンティティのインスタンスをEditviewに渡すということですよね。
新しくプロジェクトを作成する際に、
「Use Core Data」にチェックすると、
ContentView_Previewsの中でPersistenceController.preview.container.viewContextを参照していると思います。
「Persistence.swift」ファイルの中でCoreDataのデータをメモリ上に10件分生成してくれているみたいです。
Editview_Previewsでもこれを使用すると良いと思います。
次のような感じはいかがでしょうか。
swift
1struct Editview_Previews: PreviewProvider { 2 // previewのviewContextからItemのインスタンスを取得します。 3 static var item: Item { 4 let viewContext = PersistenceController.preview.container.viewContext 5 let request = Item.fetchRequest() 6 do { 7 let result = try viewContext.fetch(request) 8 return result.first! 9 } catch { 10 fatalError() 11 } 12 } 13 static var previews: some View { 14 // itemプロパティをEditviewに渡します。 15 Editview(item: item) 16 } 17}
念の為「Persistence.swift」ファイルの内容も記載しておきますね。
swift
1import CoreData 2 3struct PersistenceController { 4 static let shared = PersistenceController() 5 6 static var preview: PersistenceController = { 7 let result = PersistenceController(inMemory: true) 8 let viewContext = result.container.viewContext 9 for _ in 0..<10 { 10 let newItem = Item(context: viewContext) 11 // --- timestampの値しか設定していませんので、ご自身のItemエンティティの定義に合わせて値を設定すると良いと思います。 12 newItem.timestamp = Date() 13 } 14 do { 15 try viewContext.save() 16 } catch { 17 // Replace this implementation with code to handle the error appropriately. 18 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 19 let nsError = error as NSError 20 fatalError("Unresolved error \(nsError), \(nsError.userInfo)") 21 } 22 return result 23 }() 24 25 let container: NSPersistentContainer 26 27 init(inMemory: Bool = false) { 28 container = NSPersistentContainer(name: "PreviewPrduct") 29 if inMemory { 30 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") 31 } 32 container.loadPersistentStores(completionHandler: { (storeDescription, error) in 33 if let error = error as NSError? { 34 // Replace this implementation with code to handle the error appropriately. 35 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 36 37 /* 38 Typical reasons for an error here include: 39 * The parent directory does not exist, cannot be created, or disallows writing. 40 * The persistent store is not accessible, due to permissions or data protection when the device is locked. 41 * The device is out of space. 42 * The store could not be migrated to the current model version. 43 Check the error message to determine what the actual problem was. 44 */ 45 fatalError("Unresolved error \(error), \(error.userInfo)") 46 } 47 }) 48 container.viewContext.automaticallyMergesChangesFromParent = true 49 } 50}
投稿2023/04/15 11:25
総合スコア334
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
同じタグがついた質問を見る
Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2023/04/15 11:40
2023/04/15 12:48