如何在Textfield上使用Coredata使用@binding可选?

发布于 2025-01-31 17:33:06 字数 1342 浏览 1 评论 0原文

我有一个称为Counter的Coredata实体,我想查看用户可以更改某些值的地方。实体的一个元素被发送到editcounterview counter binding变量。我希望用户能够编辑元素标题。但是,我很难尝试使Textfield绑定到正常工作。我遇到了这两个错误:

无法转换'binding< string?'''的值的值?>'对于预期参数类型'binting< string>'

可选类型的值'fetchedResults< counter> .lement?'' (又名'可选< counter>')必须解开包装以参考包装基本类型的成员'标题'fetteDresults< counter> .lement'(又称'counter')

,与第一个错误有关不知道我如何解决这个问题,因为我一直试图弄清楚它一段时间。关于第二个错误,这表明我要么强制拆开或链条链。但是,当我这样做时,这给了我另一个错误,告诉我删除此问题。

我的代码如下:

struct EditCounterView: View {
    
    @Environment(\.managedObjectContext) var managedObjectContext
    @Environment(\.dismiss) var dismiss
    
    @Binding var editingCounter: Bool
    @Binding var counter: FetchedResults<Counter>.Element?

    var body: some View {
        NavigationView {
            VStack {
                CounterCellView(title: counter!.title!, icon: counter!.icon!, color: counter!.color!, date: counter!.date!)
                    .frame(maxWidth: .infinity, maxHeight: 80)
                    .padding()
                Form {
                    Section {
                        TextField("Required", text: $counter.title) // Where I am getting the error.
                    }
                }
            }
        }
    }
}

I have a CoreData entity called Counter, and I am wanting to make a view where the user has the ability to change some of the values. An element of the entity is sent to the EditCounterView inside the counter Binding variable. I want the user to able to edit the title of the element. However, I am having trouble trying to get the TextField binding to work properly. I get these two errors:

Cannot convert value of type 'Binding<String?>' to expected argument type 'Binding<String>'

Value of optional type 'FetchedResults<Counter>.Element?' (aka 'Optional<Counter>') must be unwrapped to refer to member 'title' of wrapped base type 'FetchedResults<Counter>.Element' (aka 'Counter')

With regards to the first error, I have no idea how I can fix this issue, as I have been trying to figure it out for a while. With regards to the second error, it suggests I either force unwrap or chain the optional. But when I do this, it gives me another error telling me to remove this.

My code is below:

struct EditCounterView: View {
    
    @Environment(\.managedObjectContext) var managedObjectContext
    @Environment(\.dismiss) var dismiss
    
    @Binding var editingCounter: Bool
    @Binding var counter: FetchedResults<Counter>.Element?

    var body: some View {
        NavigationView {
            VStack {
                CounterCellView(title: counter!.title!, icon: counter!.icon!, color: counter!.color!, date: counter!.date!)
                    .frame(maxWidth: .infinity, maxHeight: 80)
                    .padding()
                Form {
                    Section {
                        TextField("Required", text: $counter.title) // Where I am getting the error.
                    }
                }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑,眼淚并存 2025-02-07 17:33:06

我重读你的问题。这是我更正的答案。我改变了您的观点。我将计数器传递给简单LET的方式更改了。托管对象是符合observableObject的类。您可以将它们作为常数传递,并且仍然更新其值。

然后,我采用了该值,并进行了一种自定义绑定,将占据值,然后更新值。写着counter.title是可选的,但是counter本身不是。

struct EditCounterView: View {
    
    @Environment(\.managedObjectContext) var managedObjectContext
    @Environment(\.dismiss) var dismiss
    
    @Binding var editingCounter: Bool
    let counter: Counter

    var body: some View {
        NavigationView {
            VStack {
                CounterCellView(title: counter.title!, icon: counter.icon!, color: counter.color!, date: counter!.date!)
                    .frame(maxWidth: .infinity, maxHeight: 80)
                    .padding()
                Form {
                    Section {
                        // Custom Binding here...
                        TextField("Required", text: Binding<String>(
                            get: { counter.title ?? "" },
                            set: { newValue in
                                item.title = newValue
                            })
                        )
                    }
                }
            }
        }
    }
}

I reread your question. Here is my corrected answer. I changed your view a bit. I changed how counter was passed in to a simple let. Managed objects are classes conform to ObservableObject. You can pass them in as constants and still update their values.

I then took that value and made a custom binding that will take the value and then update the value. It is written presuming that counter.title is optional, but that counter itself is not.

struct EditCounterView: View {
    
    @Environment(\.managedObjectContext) var managedObjectContext
    @Environment(\.dismiss) var dismiss
    
    @Binding var editingCounter: Bool
    let counter: Counter

    var body: some View {
        NavigationView {
            VStack {
                CounterCellView(title: counter.title!, icon: counter.icon!, color: counter.color!, date: counter!.date!)
                    .frame(maxWidth: .infinity, maxHeight: 80)
                    .padding()
                Form {
                    Section {
                        // Custom Binding here...
                        TextField("Required", text: Binding<String>(
                            get: { counter.title ?? "" },
                            set: { newValue in
                                item.title = newValue
                            })
                        )
                    }
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文