I have a very simple SwiftUI app, works fine on iOS, crashes on macOS:
struct ContentView: View
{
@State var testStr: String = ""
@State var selection: TextSelection? = nil
var body: some View
{
VStack
{
TextField("Test", text: $testStr, selection: $selection)
.onChange(of: selection) {print("selection changed")}
}
.padding()
}
}
• Start app, write something in the TextField and move the cursor around.
iOS: "selection changed"
Mac: nothing (Bug ?)
• double click on some word
both Mac and iOS: "selection changed"
• write some more in the TextField
iOS: selection changed
Mac: crash
Any idea what I am doing wrong?
Gerriet.
Xcode prints:
*** +[NSSelectionArray newWithArray:]: no ranges
(
0 CoreFoundation 0x000000019b770ec0 __exceptionPreprocess + 176
1 libobjc.A.dylib 0x000000019b256cd8 objc_exception_throw + 88
2 CoreFoundation 0x000000019b770db0 +[NSException exceptionWithName:reason:userInfo:] + 0
3 AppKit 0x000000019f38138c +[NSSelectionArray newWithArray:] + 196
4 AppKit 0x000000019f3812b0 +[NSSelectionArray arrayWithArray:] + 16
5 AppKit 0x000000019f38110c -[NSTextView(NSSharing) _fixedSelectionRangesForRanges:affinity:] + 56
6 AppKit 0x000000019f3804a4 -[NSTextView(NSSharing) setSelectedRanges:affinity:stillSelecting:] + 348
...
)