If one tries to use a string property from a Coredata model on a TextField, like this:
TextEditor(text: $item.info)
will get this error
Cannot convert value of type 'Binding<String?>' to expected argument type 'Binding<String>'
So an elegant/bridge solution could be create this, so Optional String will have a binding property that you can use:
extension Optional where Wrapped == String {
var _bound: String? {
get { return self }
set { self = newValue }
}
public var bound: String {
get { return _bound ?? "" }
set { _bound = newValue.isEmpty ? nil : newValue }
}
}
TextEditor(text: $item.info.bound)
or just create a Binding var and use it on TextEditor for manipulate the property like this:
var infoEditable: Binding<String> {
Binding<String>(
get: { return item.info ?? "" },
set: { newInfo in item.info = newInfo }
)
}
TextEditor(text: infoEditable)