29 ms·
Converting a Swift String to a Bool
- Someone 4y agoI think this may be a counterexample to “Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.” Something along the lines of let r = Regex("\s*(?:(false)|true)\s*").ignoresCase() will match “false” or “true”, ignoring leading and trailing whitespace and case, and one can test whether it matched “false” or “true” by checking whether the captured group is empty. It also doesn’t produce a space-trimmed copy of the string.