简体   繁体   English

如何在使用 CoreData 的 TextField 上使用 @Binding 可选?

[英]How to use @Binding optional on TextField utilizing CoreData?

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.我有一个名为Counter的 CoreData 实体,我想创建一个视图,用户可以在其中更改某些值。 An element of the entity is sent to the EditCounterView inside the counter Binding variable.实体的一个元素被发送到counter绑定变量内的EditCounterView 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.但是,我在尝试使 TextField 绑定正常工作时遇到了麻烦。 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.
                    }
                }
            }
        }
    }
}

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 .我将counter的传递方式更改为简单的let Managed objects are classes conform to ObservableObject .托管对象是符合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.假设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
                            })
                        )
                    }
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM