title: Optional array vs. empty array in Swift
body: <p>I have a simple <code>Person</code> class in Swift that looks about like this:</p>
  
  <pre><code>class Person {
      var name = "John Doe"
      var age = 18
      var children = [Person]?
  
      \\ init function goes here, but does not initialize children array
  }
  </code></pre>
  
  <p>Instead of declaring <code>children</code> to be an optional array, I could simply declare it and initialize it as an empty array like this:</p>
  
  <pre><code>var children = [Person]()
  </code></pre>
  
  <p>I am trying to decide which approach is better. Declaring the array as an optional array means that it will not take up any memory at all, whereas an empty array has at least some memory allocated for it, correct? So using the optional array means that there will be at least some memory saving. I guess my first question is: Is there really any actual memory saving involved here, or are my assumptions about this incorrect?</p>
  
  <p>On the other hand, if it is optional then each time I try to use it I will have to check to see if it is <code>nil</code> or not before adding or removing objects from it. So there will be be some loss of efficiency there (but not much, I imagine).</p>
  
  <p>I kind of like the optional approach. Not every <code>Person</code> will have children, so why not let <code>children</code> be <code>nil</code> until the <code>Person</code> decides to settle down and raise a family?</p>
  
  <p>At any rate, I would like to know if there are any other specific advantages or disadvantages to one approach or the other. It is a design question that will come up over and over again.</p>