title: Array of nil Objects in Swift
body: <p>My class has a property that, in other languages, would be a simple Array of Strings, which would be initialized at an object's instantiation. In Swift, I have come up with the following:</p>
  
  <pre><code>class Foo {
    var myArray: (String!)[]!
  
    init(arraySize: Int, sourceOfData: SomeOtherClass){
  
      myArray = Array&lt;(String!)&gt;(count: arraySize, repeatedValue:nil)
  
      /* ... code to set the elements of the array using sourceOfData ... */
    }
  }
  </code></pre>
  
  <p>This is the only way I have been able to compile my code that allows pre-allocation of the Array's elements. However, I think all those exclamation marks make my code hard to read.</p>
  
  <p>I know I can change my <code>repeatedValue</code> to an arbitrary non-nil string, and simplify the type to <code>String[]!</code>, but that would be a hack.</p>
  
  <p>Also, I can do:</p>
  
  <pre><code>class Foo {
    let myArray: String[] = []
  
    init(sourceOfData: SomeOtherClass){
  
      /*loop over sourceOfData*/{
        myArray.append(/* computed String value */)
      }
    }
  }
  </code></pre>
  
  <p>However, this has clearly worse performance, as the compiler cannot guess the length of my Array and allocate a contiguous block of memory for it. Normally, I would not care too much about optimizing the performance of this part of my code, but for this class it is critical.</p>
  
  <p>Is there any way to have legible types without compromising performance?</p>