title: Swift - How to store in an Array subclasses of a generic type
body: <p>So, I have a generic (with restrictions) class and a number of subclasses of it that concrete the generic type when they subclass. </p>
  
  <p>I want to store instances of these subclasses in an array so then they can be iterated and treated all of them in the same way, but apparently, there is no way to convert from the subclass to the generic superclass.</p>
  
  <p>Here is some code that illustrates the problem (you can copy-paste it in a playground to see the outcome):</p>
  
  <pre><code>// Lets create regular classes
  class Fruit {
      var text: String { return "I am some Fruit" }
  }
  
  class Apple: Fruit {
      override var text: String { return "I am an Apple" }
  }
  class Orange: Fruit {
      override var text: String { return "I am an Orange" }
  }
  
  // This obviously works:
  let test1: Fruit = Apple()
  let test2: Fruit = Orange()
  
  
  // Let's create some generic class
  class Tree&lt;T: Fruit&gt; {
      let fruit: T
      init(fruit: T) {
          self.fruit = fruit
      }
  }
  
  // Subclasses from the generic class (these work)
  class AppleTree: Tree&lt;Apple&gt; {
      convenience init() {
          self.init(fruit: Apple())
      }
  }
  
  class OrangeTree: Tree&lt;Orange&gt; {
      convenience init() {
          self.init(fruit: Orange())
      }
  }
  
  // This works:
  let tree: Tree&lt;Fruit&gt; = Tree(fruit: Apple())
  tree.fruit.text               // "I am an Apple"
  
  // This works:
  let appleTree1: Tree&lt;Apple&gt; = AppleTree()
  appleTree1.fruit.text     // "I am an Apple"
  
  // This fails: "Cannot convert value of type 'AppleTree' to specified type 'Tree&lt;Fruit&gt;'
  let appleTree2: Tree&lt;Fruit&gt; = AppleTree()
  
  // This works:
  let fruitArray: [Fruit] = [Apple(), Orange()]
  
  // THIS IS MY GOAL:
  // This fails: "Cannot convert value of type 'AppleTree' to specified type 'Tree&lt;Fruit&gt;'
  let treeArray: [Tree&lt;Fruit&gt;] = [AppleTree(), OrangeTree()]
  
  
  // Let's try with a generic subclass
  class FruitTree&lt;T: Fruit&gt;: Tree&lt;T&gt;{}
  
  // This works:
  let genericTree: Tree&lt;Fruit&gt; = FruitTree(fruit: Apple())
  
  // Let's try with a generic but more concrete subclass
  class GenericOrangeTree&lt;T: Orange&gt;: Tree&lt;T&gt;{
      convenience init() {
          self.init(fruit: Orange() as! T)
      }
  }
  
  // This works:
  let genericOrangeTree1 = GenericOrangeTree(fruit: Orange())
  let genericOrangeTree2 = GenericOrangeTree()
  
  // This fails: Cannot invoke initializer for type 'GenericOrangeTree&lt;Orange&gt;' with an argument list of type '(fruit: Orange)'
  let genericTree2: Tree&lt;Fruit&gt; = GenericOrangeTree(fruit: Orange())
  
  // Again, this fails: "Cannot convert value of type 'GenericOrangeTree&lt;Orange&gt;' to specified type 'Tree&lt;Fruit&gt;'
  let genericTreeArray: [Tree&lt;Fruit&gt;] = [GenericOrangeTree()]
  </code></pre>
  
  <p>What I am trying to do is illustrated in the sample code by the <code>treeArray</code> variable.</p>
  
  <p>I don't understand why the code fails when it fails. My intuition says that this should work and I cannot find a work around this problem.</p>
  
  <p>TL;DR: I have a Generic class with some subclasses, I want to have an array of the Generic class filled with the subclasses, but the compiler complains.</p>