如何在Textfield上使用Coredata使用@binding可选?
我有一个称为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我重读你的问题。这是我更正的答案。我改变了您的观点。我将
计数器
传递给简单LET
的方式更改了。托管对象是符合observableObject
的类。您可以将它们作为常数传递,并且仍然更新其值。然后,我采用了该值,并进行了一种自定义绑定,将占据值,然后更新值。写着
counter.title
是可选的,但是counter
本身不是。I reread your question. Here is my corrected answer. I changed your view a bit. I changed how
counter
was passed in to a simplelet
. Managed objects are classes conform toObservableObject
. 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 thatcounter
itself is not.