UITextView unexpectedly calls `textView(_:shouldChangeTextIn:replacementText:)` on setting `text` property

Originator:s.h.meadley
Number:rdar://50206546 Date Originated:25/04/2019
Status:Duplicate of 48073321 (Open) Resolved:
Product:iOS + SDK Product Version:iOS 12.2 (16E227)
Classification:UIKit Reproducible:Always
 
Summary:
Setting UITextView's `text` property does not usually invoke the delegate method `textView(_:shouldChangeTextIn:replacementText:)`.

This does not appear to be true if the user taps a suggestion on the predictive keyboard. In that case, the delegate method is called twice.

Invocation 1:
    Invoked in response to the change in the text view. This is expected and is the same behaviour as typing on the keyboard.

Invocation 2:
    Invoked as part of the `text` setter. This is unwanted behaviour as we intercept replacement text via `textView(_:shouldChangeTextIn:replacementText:)`and set the text property manually. This is currently causing predictive words to appear twice.

The docs state "The text view calls this method whenever the user types a new character or deletes an existing character." which leads me to believe the second invocation is unexpected behaviour.

Steps to Reproduce:
1. Intercept replacement text in `textView(_:shouldChangeTextIn:replacementText:)` and set the text view's `text` property manually before returning `false`.

2. Tap a word on the predictive toolbar.

Expected Results:
`textView(_:shouldChangeTextIn:replacementText:)` invoked once in response to the tap on the predictive toolbar.

Actual Results:
`textView(_:shouldChangeTextIn:replacementText:)` invoked a second time, on setting the `text` property.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!