I am working on a feature where, upon completion of a form, we validate that the user has entered a "Full name". My assumption here was that I could use PersonNameComponentsFormatter
here and simply check for familyName
- if it is nil, they haven't entered a "Full name".
However, it isn't behaving quite like I had assumed. For some, seemingly random, Strings it thinks the String is a familyName
. I have reproduced this in a playground. See below
let formatter = PersonNameComponentsFormatter()
formatter.style = .default
let strings = ["test", "name", "tester", "naming", "Stefan", "stefan", "card", "dog",
"animal", "first", "string"]
for string in strings {
print(formatter.personNameComponents(from: string)?.familyName ?? "")
}
The result of which is
test
name
tester
card
first
string
Is there a bug here in the formatter or is my assumption on how to use it incorrect? i.e. should I check for both givenName
and familyName
, rather than relying on just familyName
?
While I’ll admit that the behaviour of PersonNameComponentsFormatter
is hard to explain — it looks like it has its own heuristics about what is or isn’t a viable first name — I don’t think there’s any hope of you being able to use it to achieve your overall goal. Names are just too complex for computer code to be able to reason about them reliably. For example, if you feed Quinn ”The Eskimo!”
into your validator, it would return a family name of ”The Eskimo!”
, which is definitely not right.
If you continue down this path I recommend that you use your validator to warn the user but not to block them. That is, if your validator rejects a name, don’t prevent the user from continuing, just ask them to confirm that they’ve entered their full name.
If you haven’t read it before, I recommend Falsehoods Programmers Believe About Names. And for an expansion on that, see Falsehoods Programmers Believe About Names - With Examples.
https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/
https://shinesolutions.com/2018/01/08/falsehoods-programmers-believe-about-names-with-examples/
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"