title: Generic class when inherit from UICollectionViewDataSource in swift
body: <p>when I try to create a generic class which implement <strong>UICollectionViewDataSource</strong> in swift it say that my class does not conform to protocol (and sometime Xcode crash). </p>
  
  <p>Does it mean that we can't create generic data provider for UICollectionView and that we have to duplicate code ?</p>
  
  <p>Here is the generic code :</p>
  
  <pre><code>// Enum protocol
  protocol OptionsEnumProtocol
  {
      typealias T
      static var allValues:[T] {get set}
      var description: String {get}
      func iconName() -&gt; String
  }
  
  // enum : list of first available options
  enum Options: String, OptionsEnumProtocol
  {
      typealias T = Options
  
      case Color = "Color"
      case Image = "Image"
      case Shadow = "Shadow"
  
      static var allValues:[Options] = [Color, Image, Shadow]
  
      var description: String {
          return self.rawValue
      }
  
      func iconName() -&gt; String
      {
          var returnValue = ""
  
          switch(self)
          {
              case .Color: returnValue = "color_icon"
              case .Image: returnValue = "image_icon"
              case .Shadow: returnValue = "shadow_icon"
          }
  
          return returnValue
      }
  }
  
  // class to use as the uicollectionview datasource and delegate
  class OptionsDataProvider&lt;T&gt;: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
  {
      private let items = T.allValues
  
      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int
      {
          return items.count
      }
  
      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell
      {
          let cell = collectionView.dequeueReusableCellWithReuseIdentifier(OptionsCellReuseIdentifier, forIndexPath: indexPath) as! GenericIconLabelCell
  
          let item = self.items[indexPath.row]
          // Configure the cell
          cell.iconFileName = item.iconName()
          cell.labelView.text = item.description
  
          return cell
      }
  }
  </code></pre>
  
  <p>But because it failed I have to use this non generic form instead :</p>
  
  <pre><code>enum Options: String
  {
      case Color = "Color"
      case Image = "Image"
      case Shadow = "Shadow"
  
      static var allValues:[Options] = [Color, Image, Shadow]
  
      var description: String {
          return self.rawValue
      }
  
      func iconName() -&gt; String
      {
          var returnValue = ""
  
          switch(self)
          {
              case .Color: returnValue = "color_icon"
              case .Image: returnValue = "image_icon"
              case .Shadow: returnValue = "shadow_icon"
          }
  
          return returnValue
      }
  }
  
  class OptionsDataProvider: NSObject, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
  {
      private let items = Options.allValues
  
      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int
      {
          return items.count
      }
  
      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell
      {
          let cell = collectionView.dequeueReusableCellWithReuseIdentifier(OptionsCellReuseIdentifier, forIndexPath: indexPath) as! GenericIconLabelCell
  
          let item = self.items[indexPath.row]
          // Configure the cell
          cell.iconFileName = item.iconName()
          cell.labelView.text = item.description
  
          return cell
      }
  }
  </code></pre>
  
  <p>which obligate me to duplicate the class for each enum type I have.</p>
  
  <p>Exact error :</p>
  
  <p><img src="https://i.stack.imgur.com/qxZPg.png" alt="enter image description here"></p>