【问题标题】:Value in SwiftUI not being passedSwiftUI 中的值没有被传递
【发布时间】:2021-01-29 20:01:54
【问题描述】:

我正在尝试传递一个值,让我的项目知道它应该添加到 Core Data 中的哪个组。

这是我的应用程序的一些图片。

请注意,itemsInGroup 会获取应该在组中的所有项目。

在我的应用程序中添加断点后,正在添加 Item 实体的组的值等于 nil。这应该有一个值(在按下添加项目按钮时设置)。 提前谢谢你的帮助。 代码主要部分

import SwiftUI
import CoreData

struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext
    
    enum ActiveSheet: Identifiable {
        case first, second
        
        var id: Int {
            hashValue
        }
    }

@FetchRequest(
    sortDescriptors: [NSSortDescriptor(keyPath: \Group.timestamp, ascending: true)],
    animation: .default)
private var items: FetchedResults<Group>

@State var activeSheet: ActiveSheet?
@State private var selectedGroup: Group? = nil

var body: some View {
    NavigationView {
        List {
            ForEach(items) { group in
                //                    Text("Item at \(item.timestamp!, formatter: itemFormatter)")
                Text(group.title ?? "New group")
                ForEach(group.itemsInGroup) { item in
                    Text(item.title ?? "New Item")
                }
                Button(action: {
                    selectedGroup = group
                    activeSheet = .second
                }, label: {
                    Text("Add Item")
                })
            }
            .onDelete(perform: deleteItems)
        }
        .toolbar {
            Button(action: {
                activeSheet = .first
            }) {
                Label("Add Item", systemImage: "plus")
            }
        }
    }
    .sheet(item: $activeSheet) { item in
        switch(item) {
        case .first: AddGroupName()
        case .second: AddItemView(group: selectedGroup)
        }
    }
}

private func addItem() {
    withAnimation {
        let newItem = Group(context: viewContext)
        newItem.timestamp = Date()
        
        do {
            try viewContext.save()
        } catch {
            // Replace this implementation with code to handle the error appropriately.
            // 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.
            let nsError = error as NSError
            fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
        }
    }
}

private func deleteItems(offsets: IndexSet) {
    withAnimation {
        offsets.map { items[$0] }.forEach(viewContext.delete)
        
        do {
            try viewContext.save()
        } catch {
            // Replace this implementation with code to handle the error appropriately.
            // 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.
            let nsError = error as NSError
            fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
        }
    }
}
}

private let itemFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    formatter.timeStyle = .medium
    return formatter
}()

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
    }
}

我在哪里添加新项目

struct AddItemView: View {
@Environment(\.managedObjectContext) private var viewContext

let group: Group?
@State var title = ""
var body: some View {
    VStack {
        Form {
            TextField("Title", text: $title)
            Button(action: {
                let item = Item(context: viewContext)
                item.group = group
                item.title = title
                try? viewContext.save()
            }, label: {
                Text("Save")
            })
        }
    }
}
}

我在哪里添加新组

struct AddGroupName: View {
@Environment(\.managedObjectContext) private var viewContext

@State var title = ""

var body: some View {
    VStack {
        Form {
            TextField("Title", text: $title)
            Button(action: {
                let item = Group(context: viewContext)
                item.title = title
                item.timestamp = Date()
                try? viewContext.save()
            }, label: {
                Text("Save")
            })
        }
        
    }
}
}

我的核心数据模型

为什么没有正确传递和保存组值?它应该保存在代码主要部分的 selectedGroup 变量中。

当我尝试添加一个项目并将其保存到核心数据数据库时,我收到此错误“非法尝试在不同上下文中的对象之间建立关系‘组’”

请注意,我尝试将 selectedGroup 设置为 nil 以外的值,但是在尝试添加项目时会使用此初始值。

【问题讨论】:

    标签: swift xcode swiftui


    【解决方案1】:

    您应该使用AddItemView 中的@Binding 属性包装器声明您的group 属性

     @Binding var group: Group
    

    这样@State 属性selectedGroup 将在AddItemView 被解除时更新

    【讨论】:

    • @NightWing,对此有何反馈?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 2020-01-29
    • 1970-01-01
    • 2013-02-24
    相关资源
    最近更新 更多