title: Swift variable type setting to superclass handle subclasses
body: <p>How do I work with variables in Swift that have the type of a main class but are passed an instance of a subclass?</p>
  <p>Here is a piece of example code:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>class MainClass {
      var name: String
      
      init(name: String) {
          self.name = name
      }
  }
  
  class Num1: MainClass {
      var num: Int = 1
  }
  
  class Num2: MainClass {
      var num: Int = 2
  }
  
  struct ExampleView: View {
      var subClassInstance: MainClass
      
      var body: some View {
          Text(subClassInstance.name)
          Text(subClassInstance.num) // trying to access this property
      }
  }
  let example = ExampleView(subClassInstance: Num1(name: "test"))</code></pre>
  </div>
  </div>
  </p>
  <p>Specifically, I want to be able to access subclass properties from a variable with the type of a main class. In the context of my example, I want to be able to access the &quot;num&quot; property of the passed subclass instance from the variable in the view with type of MainClass. Is this possible? The motivation for doing this is having one view that works with similar subclasses of a main class--not having to write two views, one with a variable set to the type of each subclass.</p>