<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">You are perfectly right. The context is playing around really, but I was more specifically writing a function counting vowels and consonants in an arbitrary string:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> countLetters(s: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> (vowels: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">, consonants: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> vowels: </span><span style="font-variant-ligatures: no-common-ligatures" class="">Set</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures" class="">Character</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> = </span><span style="font-variant-ligatures: no-common-ligatures" class="">Set</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"AEIOU"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures" class="">characters</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> consonants: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Set</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Character</span><span style="font-variant-ligatures: no-common-ligatures" class="">> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Set</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"BCDFGHJKLMNPQRSTVWXYZ"</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">characters</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> v = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">, c = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> char </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> s.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">uppercased</span><span style="font-variant-ligatures: no-common-ligatures" class="">().</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">characters</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> vowels.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">contains</span><span style="font-variant-ligatures: no-common-ligatures" class="">(char) {</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> v += </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> consonants.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">contains</span><span style="font-variant-ligatures: no-common-ligatures" class="">(char) {</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> c += </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (v, c)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class="">As you could see, I opted not to use CharacterSet for this case, as it looked too much trouble. </div><div class=""><br class=""></div><div class="">The current goal is for me to learn Swift. Trying to extrapolate a bit on what might happen in the real world, I would tend to answer your questions thus:</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class="">* Do you plan to use a fixed character set? Or is the character set itself built at runtime?<br class=""></blockquote><br class=""></div><div class="">The character set is likely to be fixed. Does this really change anything?</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class="">* Do you have specific knowledge of either of the inputs? Like that they’re all ASCII? Or normalised in a certain way?<br class=""></blockquote><br class=""></div><div class="">ASCII? Probably not. Latin? perhaps, though not obvious. For example French accented letters would probably have to be handled somehow. Greek or Cyrillic? Perhaps. Other scripts? Unlikely, but what do I know.</div><div class="">Normalisation: it should probably consider all variations of “é” to be the same…</div><div class="">Is this opening a Unicode can of worms? Possibly. I am not knowledgeable enough, but willing to learn.</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class="">* Specifically, where do the characters you’re trying to test (`char` in your example) come from? Do they represent user input, in which case they can be arbitrary Unicode? Or something more constrained<br class=""></blockquote></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">User input most probably.</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">I tried and your suggestion:</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><blockquote type="cite" class="">let uchar: UnicodeScalar = “E"<br class=""></blockquote><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">will not work with a Character variable (as opposed to a character literal)</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> uchar: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UnicodeScalar</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = char</span></div></span></div><div class=""><br class=""></div><div class="">(Cannot convert value of type Character to specified type UnicodeScalar)</div><div class=""><br class=""></div><div class="">Thanks,</div><div class=""><br class=""></div><div class="">Jean-Denis </div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On 3 Oct 2016, at 09:43, Quinn The Eskimo! via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class="">On 2 Oct 2016, at 19:02, Jean-Denis Muys via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><br class=""><blockquote type="cite" class="">The problem is, I could not find a simple way to convert from a character to a unicodeScalar. <br class=""></blockquote><br class="">As is often the case with string examples, it would help if you posted more about your context. With the details we have now your code could be written like this:<br class=""><br class="">let vowels = CharacterSet(charactersIn: "AEIOU")<br class="">let char: UnicodeScalar = "E"<br class="">vowels.contains(char)<br class=""><br class="">but I’m pretty sure that won’t help in your real app (-: So, my questions:<br class=""><br class="">* Do you plan to use a fixed character set? Or is the character set itself built at runtime?<br class=""><br class="">* Do you have specific knowledge of either of the inputs? Like that they’re all ASCII? Or normalised in a certain way?<br class=""><br class="">* Specifically, where do the characters you’re trying to test (`char` in your example) come from? Do they represent user input, in which case they can be arbitrary Unicode? Or something more constrained<br class=""><br class="">Share and Enjoy<br class="">--<br class="">Quinn "The Eskimo!" <<a href="http://www.apple.com/developer/" class="">http://www.apple.com/developer/</a>><br class="">Apple Developer Relations, Developer Technical Support, Core OS/Hardware<br class=""><br class=""><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></div></blockquote></div><br class=""></body></html>