This repository has been archived by the owner on May 6, 2023. It is now read-only.
Last character in TextView doesn't delete when the delete key is used. #7
Labels
bug
Something isn't working
Have you read the Contributing Guidelines?
Yes
General Information
The issue I see seems very strange to me, in that deleting the last character with the delete key is a fundamental function. Yet, it doesn't work in the situation I'm using it.
What I'm looking for is confirmation that what I'm seeing is a fundamental bug, or just something that is happening in the complex CoreData, @ObservedObject, and SwiftUI architecture.
Platform/OS version:
iOS 15.x. MacOS Cayalist
IDE version:
Xcode 13.2.1
Devices:
iOS and iOS simulator
Any related GitHub issues:
Describe the bug
After trying a few characters into a TextView and then deleting them using the delete key, all the characters will delete except the last. What happens to the last, is the cursor will move to the 0 position, but the last character remains.
Also, related is if you have one or more characters and you select them and either hit the delete key, or use the Cut menu item, the text remains as described above.
Steps to reproduce
See above.
Expected behavior
The last character to delete.
Stack trace, compiler error, code snippets
None
Screenshots
None
Additional context
My text is coming from a CoreData text string, from an @ObservedObject var dataElement: [CoreDataObjectWithText]
Usage:
TextView(Binding(self.$dataElement.text, replacingNilWith: ""))
.isEditable(true)
Below is the custom Binding extension.
public extension Binding where Value: Equatable {
/// Creates a non-nil binding by projecting to its unwrapped value, translating nil values
/// to or from the given nil value. If the source contains nil, this binding will return the
/// nil value. If this binding is set to the given nil value, it will assign nil to the underlying
/// source binding.
///
/// This is useful if you have optional values of a type that has a logical 'empty' value of
/// its own, for example
String
:///
/// @State var name: String?
/// ...
/// TextField(text: Binding($name, replacingWithNil: ""))
///
/// If the
name
property containsnil
, the text field will see an empty string. If the text field/// assigns an empty string, the
name
property will be set tonil
.@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
init(_ source: Binding<Value?>, replacingNilWith nilValue: Value) {
self.init(
get: { source.wrappedValue ?? nilValue },
set: { newValue in
if newValue == nilValue {
source.wrappedValue = nil
}
else {
source.wrappedValue = newValue
}
})
}
The text was updated successfully, but these errors were encountered: