我正在尝试使用swiftui和核心数据创建一个简单的任务处理应用程序,但是,添加视图时遇到了一些问题。我有一个TaskView
视图,该视图将用于显示列表中有关任务的一些信息(标题,日期等)。但是,当尝试使用演示Task
对象为视图创建预览时,预览会崩溃(正在运行的应用仍然有效)。
很抱歉,如果这是一个琐碎的问题,那么它通常是快速入门和编程的。
TaskView.swift
import SwiftUI
struct TaskView: View {
@Environment(\.managedObjectContext) var managedObjectContext
var task: Task
var body: some View {
HStack {
Image(systemName: task.checked == true ? "circle" : "checkmark.circle.fill")
VStack {
Text(task.name ?? "unknown name")
Text(String(task.streak))
}
}
}
}
struct TaskView_Previews: PreviewProvider {
static var previews: some View {
let task = Task()
task.name = "Washing up"
task.desc = "Wash the dishes"
task.checked = false
task.streak = 2
return TaskView(task: task)
.previewLayout(.fixed(width: 375, height: 60))
}
}
ContentView.swift
import SwiftUI
struct ContentView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: Task.entity(), sortDescriptors: [
NSSortDescriptor(keyPath: \Task.checked, ascending: false),
NSSortDescriptor(keyPath: \Task.name, ascending: true)
]) var tasks: FetchedResults<Task>
var body: some View {
NavigationView {
List {
ForEach(tasks, id: \.self) { task in
TaskView(task: task).environment(\.managedObjectContext, self.managedObjectContext)
}
.onDelete(perform: deleteTask)
}
.navigationBarTitle("Today")
}
}
func deleteTask(at indexSet: IndexSet) {
indexSet.forEach { TaskUtils.delete(task: tasks[$0], using: self.managedObjectContext)}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
答案 0 :(得分:1)
我通过添加一个设置上下文常量,然后使用init初始化您在预览中使用的任务常量来修复TaskView的方法
import SwiftUI
struct TaskView: View {
@Environment(\.managedObjectContext) var managedObjectContext
var task: Task
var body: some View {
HStack {
Image(systemName: task.checked == true ? "circle" : "checkmark.circle.fill")
VStack {
Text(task.name ?? "unknown name")
Text(String(task.streak))
}
}
}
}
struct TaskView_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext // IMPORTANT
let task = Task.init(context: context) // IMPORTANT
task.name = "Washing up"
task.desc = "Wash the dishes"
task.checked = false
task.streak = 2
return TaskView(task: task).environment(\.managedObjectContext, context)
.previewLayout(.fixed(width: 375, height: 60))
}
}
对于您的ContentView,我做了一些小的改动以使其正常工作-添加了一个按钮,用于向CoreData模型添加对象。
import SwiftUI
struct ContentView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: Task.entity(), sortDescriptors: [
NSSortDescriptor(keyPath: \Task.checked, ascending: false),
NSSortDescriptor(keyPath: \Task.name, ascending: true)
]) var tasks: FetchedResults<Task>
var body: some View {
NavigationView {
List {
ForEach(tasks, id: \.self) { task in
TaskView(task: task).environment(\.managedObjectContext, self.managedObjectContext)
}
//.onDelete(perform: deleteTask)
}
.navigationBarTitle("Today")
.navigationBarItems(trailing:
Button("Add") {
let task = Task(context: self.managedObjectContext)
task.name = "Washing up"
task.desc = "Wash the dishes"
task.checked = false
task.streak = Int32(Int.random(in: 0...10))
try? self.managedObjectContext.save()
}
)
}
}
// func deleteTask(at indexSet: IndexSet) {
// indexSet.forEach { TaskUtils.delete(task: tasks[$0], using: self.managedObjectContext)}
// }
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
return ContentView().environment(\.managedObjectContext, context)
}
}