title: Does Swift have a null coalescing operator and if not, what is an example of a custom operator?
body: <p>A common feature in many languages, the <a href="https://en.wikipedia.org/wiki/Null_coalescing_operator" rel="noreferrer">Null Coalescing Operator</a>, is a binary operator often used to shorten expressions of the type:</p>
  
  <pre><code>x = possiblyNullValue NCO valueIfNull
  </code></pre>
  
  <p>…where <code>NCO</code> is a placeholder for the language’s null coalescing operator. </p>
  
  <p>Objective C's Null Coalescing Operator is <code>?:</code>, so the expression would be:</p>
  
  <pre><code>x = possiblyNullValue ?: valueIfNull
  </code></pre>
  
  <p>The above expression is also equivalent to the use of tertiary operator:</p>
  
  <pre><code> x =  someTestForNotNull( possiblyNullValue ) ? possiblyNullValue : valueIfNull
  </code></pre>
  
  <h2>Advantages of a Null Coalescing Operator</h2>
  
  <ul>
  <li>More readable code (especially with long, descriptive variable names)</li>
  <li>Reduced possibility of typographic errors (tested var is typed only once)</li>
  <li>No double evaluation of the tested variable where the tested variable is a getter, since its accessed once (or the need to cache it to intentionally avoid double evaluation).</li>
  </ul>