The simplified scenario is the following.
(888) 555-5512)
] to the UITextField.The number will be added with a Unicode character at the beginning and at the end, getting like \u{e2}(888) 555-5512\u{e2}
when exploring the variable while debugging.
This is really weird and in my opinion, not the intended behaviour. Is this a bug or something that works intentionally this way?
Code:
Nothing complicated here. As described before, brand new project, add UITextField, add Button, and if button triggered print the result.
The print will show the phone just fine, just put a breakpoint in the print
line and see the value of the phone var to see what I mean.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var phoneLabel: UITextField!
@IBAction func goButton(_ sender: UIButton) {
let text = phoneLabel.text ?? ""
print(text)
}
}
Tested in:
Steps with images:
This is what I got at the breakpoint line.
Apple offers robust support in macOS for Unicode, a standard that provides a unique number to represent characters and symbols. Unicode encompasses scripts used by languages, symbols for scientific notation, emoji, and other kinds of marks.
Adding a Unicode keyboard To install a Unicode keyboard on your iPhone or iPad, launch the App Store and download the free UniChar app. Then launch Settings, General, Keyboard, Keyboards, Add New Keyboard…, and select UniChar from the options.
I had exactly the same issue recently. Interestingly enough what you see in the debugger is unfortunately not what is actually pasted. If you copy the number to a different place and investigate it with your console for example you will get the following output:
>>> u"\U+202D(888) 5555-5512\U+202C"
u'\u202d(888) 5555-5512\u202c'
>>> name(u"\U+202D")
'LEFT-TO-RIGHT OVERRIDE'
>>> name(u"\U+202C")
'POP DIRECTIONAL FORMATTING'
So as you can see it is really two different invisible characters controlling the flow of the text.
In order to solve that I filtered out all unicode characters of the cF
category. So you could do:
phoneLabel.text?.replacingOccurrences(of: "\\p{Cf}", with: "", options: .regularExpression)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With