title: Working with Unicode code points in Swift
body: <p>If you are not interested in the details of Mongolian but just want a quick answer about using and converting Unicode values in Swift, then skip down to the first part of the <a href="https://stackoverflow.com/a/31483262/3681880">accepted answer</a>.</p>
  
  <hr>
  
  <h1>Background</h1>
  
  <p>I want to render Unicode text for <a href="https://stackoverflow.com/questions/28544714/how-do-you-make-a-vertical-text-uilabel-and-uitextview-for-ios-in-swift">traditional Mongolian to be used in iOS apps</a>. The better and long term solution is to use an <a href="https://en.wikipedia.org/wiki/Apple_Advanced_Typography" rel="noreferrer">AAT smart font</a> that would render this complex script. (<a href="http://www.mongolfont.com/en/font/mnglwhiteaat.html" rel="noreferrer">Such fonts do exist</a> but their license does not allow modification and non-personal use.) However, since I have never made a font, let alone all of the rendering logic for an AAT font, I just plan to do the rendering myself in Swift for now. Maybe at some later date I can learn to make a smart font. </p>
  
  <p>Externally I will use Unicode text, but internally (for display in a <code>UITextView</code>) I will convert the Unicode to individual glyphs that are stored in a dumb font (coded with Unicode <a href="https://en.wikipedia.org/wiki/Private_Use_Areas" rel="noreferrer">PUA</a> values). So my rendering engine needs to convert Mongolian Unicode values (range: U+1820 to U+1842) to glyph values stored in the PUA (range: U+E360 to U+E5CF). Anyway, this is my plan since it is <a href="https://github.com/suragch/Chimee/blob/master/app/src/main/java/net/studymongolian/chimee/MongolUnicodeRenderer.java" rel="noreferrer">what I did in Java in the past</a>, but maybe I need to change my whole way of thinking.</p>
  
  <h1>Example</h1>
  
  <p>The following image shows <em>su</em> written twice in Mongolian using two different forms for the letter <em>u</em> (in red). (Mongolian is written vertically with letters being connected like cursive letters in English.) </p>
  
  <p><img src="https://i.stack.imgur.com/xtEJJ.png" alt="enter image description here"></p>
  
  <p>In Unicode these two strings would be expressed as </p>
  
  <pre><code>var suForm1: String = "\u{1830}\u{1826}"
  var suForm2: String = "\u{1830}\u{1826}\u{180B}"
  </code></pre>
  
  <p>The Free Variation Selector (U+180B) in <code>suForm2</code> is recognized (correctly) by Swift <code>String</code> to be a unit with the <em>u</em> (U+1826) that precedes it. It is considered by Swift to be a single character, an extended grapheme cluster. However, for the purposes of doing the rendering myself, I need to differentiate <em>u</em> (U+1826) and FVS1 (U+180B) as two distinct UTF-16 code points.</p>
  
  <p>For internal display purposes, I would convert the above Unicode strings to the following rendered glyph strings:</p>
  
  <pre><code>suForm1 = "\u{E46F}\u{E3BA}" 
  suForm2 = "\u{E46F}\u{E3BB}"
  </code></pre>
  
  <h1>Question</h1>
  
  <p>I have been playing around with Swift <code>String</code> and <code>Character</code>. There are a lot of convenient things about them, but since in my particular case I deal exclusively with UTF-16 code units, I wonder if I should be using the old <code>NSString</code> rather than Swift's <code>String</code>. I realize that I can use <code>String.utf16</code> to get UTF-16 code points, but <a href="https://stackoverflow.com/questions/24542170/is-there-a-way-to-create-a-string-from-utf16-array-in-swift">the conversion back to <code>String</code> isn't very nice</a>.</p>
  
  <p>Would it be better to stick with <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_String_Structure/index.html" rel="noreferrer"><code>String</code></a> and <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_Character_Structure/index.html" rel="noreferrer"><code>Character</code></a> or should I use <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/index.html" rel="noreferrer"><code>NSString</code></a> and <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/index.html#//apple_ref/c/tdef/unichar" rel="noreferrer"><code>unichar</code></a>?</p>
  
  <h1>What I have read</h1>
  
  <ul>
  <li><a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html" rel="noreferrer">Strings and Characters documentation</a></li>
  <li><a href="http://oleb.net/blog/2014/07/swift-strings/" rel="noreferrer">Strings in Swift</a></li>
  <li><a href="http://www.objc.io/issues/9-strings/unicode/" rel="noreferrer">NSString and Unicode</a></li>
  </ul>
  
  <p><em>Updates to this question have been hidden in order to clean the page up. See the edit history.</em></p>