title: How to make return type of function generic in Swift
body: <h2>Intro</h2>
  
  <p>In my app, I have one <strong>superclass called "ElementData"</strong> and <em>several child classes that inherit from it.</em></p>
  
  <blockquote>
    <p>Each child class has its own validateModel() method which returns
    a different type, depending on the class - always in an Array.</p>
  </blockquote>
  
  <p><strong>In other words:</strong> the method returns different Types in every subclass.</p>
  
  <h2>Example</h2>
  
  <p><strong>Class A:</strong> <code>func validateModel() -&gt; [String]</code></p>
  
  <p><strong>Class B:</strong> <code>func validateModel() -&gt; [Int]</code></p>
  
  <p><strong>Class C:</strong> <code>func validateModel() -&gt; [MyCustomEnum]</code></p>
  
  <p>As you can see, only the return values differ from each other.</p>
  
  <p><strong>EDIT: Examples of validateModel() method:</strong></p>
  
  <p><strong>Class A:</strong></p>
  
  <pre><code>func validateModel() -&gt; [DefaultElementFields]{ // DefaultElementFields is an enum with the different view types of my collection view
  
          var checkResult: [DefaultElementFields] = []
  
          if name == "" {
              checkResult.append(.Name)
          }
  
          if Int(rewardedPoints) == nil {
              checkResult.append(.Points)
          }
  
          if description == "" {
              checkResult.append(.Description)
          }
  
          if selectedImage == nil {
              checkResult.append(.Image)
          }
  
          return checkResult
      }
  </code></pre>
  
  <p><strong>Class B:</strong></p>
  
  <pre><code>func validateModel() -&gt; [Int] { // returns the index of the text field which is wrong
          var checkResult: [Int] = []
  
          let filledValues = codes.filter {
              $0 != ""
          }
  
          if filledValues.count == 0 { // if no values have been entered, all fields should be marked red.
              checkResult.append(-1)
              return checkResult
          }
  
  
          for (i, code) in codes.enumerated() {
              if code != "" &amp;&amp; (code.count &lt; 3 || code.count &gt; 10 || code.rangeOfCharacter(from: NSCharacterSet.alphanumerics.inverted) != nil){ // code must have more than 3 and less than 11 characters. No symbols are allowed.
                  checkResult.append(i)
              }
          }
  
  
  
          return checkResult
      }
  </code></pre>
  
  <p><strong>EDIT: What the classes are for:</strong></p>
  
  <p>The classes basically <strong>store data</strong> a user entered <strong>into collection view cells</strong>, e.g. text, numbers or dates. Each CollectionViewCellType has its own class. Due to <strong>reuse behavior</strong> of the collection view, it is necessary to store the entered values in a <strong>model</strong>.</p>
  
  <p>The <strong>model</strong> also <strong>takes care of the validation</strong> and returns - depending on the cell - an array of values that tells the cell which fields should get a red border (be marked as invalid).</p>
  
  <p>This can sometimes be an Enum, an Int or a String.</p>
  
  <h2>What I would like to achieve</h2>
  
  <p>As you might imagine, it is quite annoying to have almost the same validationMethods in each <strong>child-class</strong> as it is necessary to <strong>downcast</strong> every time I want to use the method on one of the classes.</p>
  
  <p>Therefore, I would like to keep the <strong>return type open</strong> i.e. don't specify a specific one in the parent class as the subclass should be able to return any type. Then, I would move the <strong>validateModel()</strong> method into the parent class and override the method in its subclasses.</p>
  
  <p>I thought of a solution with <strong>generics</strong> (if it's possible).</p>
  
  <h2>What I tried</h2>
  
  <p><strong>Here is my generic approach for the whole thing:</strong></p>
  
  <pre><code>class ElementData {
  
      func validateModel&lt;T&gt;() -&gt; [T] {
          return [1] as! [T] // just a test return
      }
  
  }
  </code></pre>
  
  <p><strong>And the call of the method:</strong></p>
  
  <pre><code>dataObject.validateModel() // dataObject inherits from ElementData -&gt; has access to validateModel()
  </code></pre>
  
  <p>Unfortunately, it does not work and I get the following error:</p>
  
  <blockquote>
    <p>"Generic parameter 'T' could not be inferred"</p>
  </blockquote>
  
  <h2>Summary:</h2>
  
  <ul>
  <li>I have one <strong>superclass</strong> "ElementData" and several subclasses (classes that inherit)</li>
  <li>Each <strong>subclass has a method validateModel()</strong> in which the model is validated</li>
  <li>Only the <strong>return types</strong> of the validateModel() methods inside the subclasses <strong>differ</strong> - so I would like to put the method inside the parent class (ElementData) and just <strong>override</strong> it on the subclasses</li>
  </ul>
  
  <p><strong>Is that possible, if so, how?</strong></p>
  
  <p>Any help would be appreciated.</p>