title: Why do I need to declare an optional value as nil explicitly in Struct - Swift
body: <p>Here's a quote from the docs:</p>
  
  <blockquote>
    <p>If your custom type has a stored property that is logically allowed to have “no value”—perhaps because its value cannot be set during initialization, or because it is allowed to have “no value” at some later point—declare the property with an optional type. Properties of optional type are automatically initialized with a value of nil, indicating that the property is deliberately intended to have “no value yet” during initialization.</p>
  </blockquote>
  
  <p>If I do this with a class it works fine:</p>
  
  <pre><code>class MyClass {
      var someProperty: String?
  }
  
  var myClass = MyClass()
  myClass.someProperty // Shows nil
  </code></pre>
  
  <p>However, if I do this with a struct type, I get an error on initialization:</p>
  
  <pre><code>struct MyStruct {
      var someProperty: String?
  }
  
  // ERROR
  var myStruct = MyStruct()
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>Missing argument for parameter 'someProperty'</p>
  </blockquote>
  
  <p>I can remedy this by declaring it nil explicitly like so:</p>
  
  <pre><code>struct MyStruct {
      var someProperty: String? = nil
  }
  
  // Valid
  var myStruct = MyStruct()
  </code></pre>
  
  <h3> Question </h3>
  
  <p>Given the documentation, I would expect properties on any type that are set as optionals to be defaulted to nil.  Is there a reason I have to declare it explicitly on a struct?</p>
  
  <p>Why?</p>
  
  <p>No good reason, like many of you, I'm just experimenting.</p>