The behavior you're observing is due to the way Swift handles characters in a string when checking if they are members of a CharacterSet. When you create a CharacterSet using CharacterSet(charactersIn: input), it considers the individual characters in the input string, not the entire string as a single unit.
In your example:
let input11 = "a🥀"
When you create a CharacterSet from input11, it includes the characters "a" and "🥀". When you check if this character set is a subset of CharacterSet.letters, it evaluates to true because "a" is indeed a letter.
In contrast, when you directly check if CharacterSet.letters is a superset of the character set created from "a🥀", it correctly evaluates to false because "a🥀" is not entirely composed of letters.
This behavior is consistent with how Swift handles characters and character sets. If you want to check if all characters in a string are letters, you can iterate over the string and check each character individually. For example:
let input11 = "a🥀"
let allLetters = input11.allSatisfy { $0.isLetter }
print("'\(input11)' contains only letters: \(allLetters)")
This will correctly output false for "a🥀" because it checks each character individually.
Topic:
App & System Services
SubTopic:
General
Tags: