3

我在 SwiftUI 中有两个页面,可以使用 tabView 进行切换,一个是 ProjectPage,另一个是 HabitPage。在 ProjectPage 中,可以显示项目列表。在 HabitPage 中,可以显示习惯列表。也可以在此页面中添加新习惯。所有的项目和习惯都将存储在核心数据中。项目与任务有关系,任务与项目有关系。在我建立一个新习惯并切换到项目页面后,程序会崩溃。

输出

2020-01-14 14:03:51.156969+1100 Demo9R[35096:12471800] precondition failure: attribute failed to set an initial value: 74

我的代码的一部分:

struct ProjectView: View {
@Environment(\.managedObjectContext) var managedObjectContext

@FetchRequest(entity: Project.entity(), sortDescriptors: []) var projects: FetchedResults<Project>

@State var popAddProject: Bool = false

var body: some View {
    ZStack {
        NavigationView {
            List {
                ForEach(projects, id: \.self) { project in
                    NavigationLink(destination: ProjectProfile(project: project).environment(\.managedObjectContext, self.managedObjectContext)) {
                        ProjectCard(name: project.wrappedName, tasks: project.tasksName)
                    }
                }
            }
            .navigationBarTitle("Project")
        }

        PlusButton() {
            self.popAddProject.toggle()
        }

        if self.popAddProject {
            MiddleLayer(ifDisapper: self.$popAddProject)

            AddProjectView(ifAppear: self.$popAddProject)
                .background(Color.white)
                .shadow(radius: 16)
                .cornerRadius(12.0)
                .padding()
                .environment(\.managedObjectContext, managedObjectContext)

        }
    }
}

}

习惯页面:

struct HabitView: View {
@Environment(\.managedObjectContext) var managedObjectContext

@FetchRequest(entity: Habit.entity(), sortDescriptors: []) var habits: FetchedResults<Habit>
@FetchRequest(entity: Task.entity(), sortDescriptors: []) var tasks: FetchedResults<Task>

@State var popAddHabit = false

var body: some View {
    ZStack {
        NavigationView {
            List {
                ForEach(self.habits, id: \.self) { habit in
                    HabitCard(habit: habit)
                }
            }
            .navigationBarTitle("Habits")
        }

        PlusButton {
            self.popAddHabit.toggle()
        }
        .disabled(self.tasks.count == 0)

        if self.popAddHabit {
            MiddleLayer(ifDisapper: self.$popAddHabit)

            AddHabitView(ifAppear: self.$popAddHabit)
        }
    }
}

}

内容视图

struct ContentView: View {
@State private var selection = 0

var body: some View {
    TabView(selection: $selection){
        ProjectView()
            .font(.title)
            .tabItem {
                VStack {
                    Image("first")
                    Text("Project")
                }
            }
            .tag(2)
        HabitView()
            .font(.title)
            .tabItem {
                VStack {
                    Image("second")
                    Text("Second")
                }
            }
            .tag(1)
        TodayView()
            .font(.title)
            .tabItem {
                VStack {
                    Text("Third")
                }
            }
            .tag(0)
    }
    .edgesIgnoringSafeArea(.top)
}

}

谁能告诉我为什么?我已经尝试了很长时间

4

0 回答 0