title: Separating Data Source to another class in Swift
body: <p>I'm trying to keep my view controllers clean as described in this article <a href="http://www.objc.io/issue-1/lighter-view-controllers.html">objc.io Issue #1 Lighter View Controllers</a>. I tested this method in Objective-C and it works fine. I have a separate class which implements <code>UITableViewDataSource</code> methods.</p>
  
  <pre><code>#import "TableDataSource.h"
  
  @interface TableDataSource()
  
  @property (nonatomic, strong) NSArray *items;
  @property (nonatomic, strong) NSString *cellIdentifier;
  
  @end
  
  @implementation TableDataSource
  
  - (id)initWithItems:(NSArray *)items cellIdentifier:(NSString *)cellIdentifier {
      self = [super init];
      if (self) {
          self.items = items;
          self.cellIdentifier = cellIdentifier;
      }
      return self;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      return self.items.count;
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
  
      cell.textLabel.text = self.items[indexPath.row];
  
      return cell;
  }
  
  @end
  </code></pre>
  
  <p>From the tableview controller, all I have to do is instantiate a instance of this class and set it as the tableview's data source and it works perfectly.</p>
  
  <pre><code>self.dataSource = [[TableDataSource alloc] initWithItems:@[@"One", @"Two", @"Three"] cellIdentifier:@"Cell"];
  self.tableView.dataSource = self.dataSource;
  </code></pre>
  
  <p>Now I'm trying to do the same in Swift. First here's my code. Its pretty much of a translation of the Objective-C code above.</p>
  
  <pre><code>import Foundation
  import UIKit
  
  public class TableDataSource: NSObject, UITableViewDataSource {
  
      var items: [AnyObject]
      var cellIdentifier: String
  
      init(items: [AnyObject]!, cellIdentifier: String!) {
          self.items = items
          self.cellIdentifier = cellIdentifier
  
          super.init()
      }
  
      public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return items.count
      }
  
      public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell
  
          cell.textLabel?.text = items[indexPath.row] as? String
  
          return cell
      }
  
  }
  </code></pre>
  
  <p>And I call it like this.</p>
  
  <pre><code>let dataSource = TableDataSource(items: ["One", "Two", "Three"], cellIdentifier: "Cell")
  tableView.dataSource = dataSource
  </code></pre>
  
  <p>But the app crashes with the following error.</p>
  
  <p><strong>-[NSConcreteNotification tableView:numberOfRowsInSection:]: unrecognized selector sent to instance</strong></p>
  
  <p>I checked the <code>init</code> method of <code>TableDataSource</code> and the items and the cell identifier gets passed fine. I had to declare the <code>UITableViewDataSource</code> methods <code>public</code> and remove the <code>override</code> keyword otherwise it would give compile time errors.</p>
  
  <p>I'm clueless on what's going wrong here. Can anyone please help me out?</p>
  
  <p>Thank you.</p>