title: Separating CamelCase string into space-separated words in Swift
body: <p>I would like to separate a CamelCase string into space-separated words in a new string. Here is what I have so far:</p>
  
  <pre><code>var camelCaps: String {
      guard self.count &gt; 0 else { return self }
      var newString: String = ""
  
      let uppercase = CharacterSet.uppercaseLetters
      let first = self.unicodeScalars.first!
      newString.append(Character(first))
      for scalar in self.unicodeScalars.dropFirst() {
          if uppercase.contains(scalar) {
              newString.append(" ")
          }
          let character = Character(scalar)
          newString.append(character)
      }
  
      return newString
  }
  
  let aCamelCaps = "aCamelCaps"
  let camelCapped = aCamelCaps.camelCaps // Produce: "a Camel Caps"
  
  let anotherCamelCaps = "ÄnotherCamelCaps"
  let anotherCamelCapped = anotherCamelCaps.camelCaps // "Änother Camel Caps"
  </code></pre>
  
  <p>I'm inclined to suspect that this may not be the most efficient way to convert to space-separated words, if I call it in a tight loop, or 1000's of times. Are there more efficient ways to do this in Swift?</p>
  
  <p>[Edit 1:] The solution I require should remain general for Unicode scalars, not specific to Roman ASCII "A..Z". </p>
  
  <p>[Edit 2:] The solution should also skip the first letter, i.e. not prepend a space before the first letter.</p>
  
  <p>[Edit 3:] Updated for Swift 4 syntax, and added caching of uppercaseLetters, which improves performance in very long strings and tight loops.</p>