<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>NimbusKit</title>
    <description></description>
    <link>http://nimbuskit.github.io/</link>
    <atom:link href="http://nimbuskit.github.io//feed.xml" rel="self" type="application/rss+xml" />
    
      <item>
        <title>Swift Gotchas</title>
        <description>&lt;p&gt;A collection of gotchas encountered while porting NimbusKit to Swift.&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h2 id=&quot;classes-cant-be-cast-as-objects-that-conform-to-protocols&quot;&gt;Classes can’t be cast as objects that conform to protocols&lt;/h2&gt;

&lt;p&gt;Swift’s type-safety generally leads to cleaner code at the expense of hackery that was possible in Objective-C.&lt;/p&gt;

&lt;p&gt;NimbusKit encouraged the following pattern when creating a &lt;code&gt;NITableViewModel&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-objectivec&quot;&gt;_model = [[NITableViewModel alloc] initWithSectionedArray:contents delegate:(id)[NICellFactory class]];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;NICellFactory implemented &lt;code&gt;NITableViewModelDelegate&lt;/code&gt; both as instance methods &lt;strong&gt;and&lt;/strong&gt; as class methods. This treated &lt;code&gt;NICellFactory.class&lt;/code&gt; like a singleton that implemented &lt;code&gt;NITableViewModelDelegate&lt;/code&gt;. While convenient, it was not necessarily &lt;em&gt;safe&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In Swift you cannot do an &lt;code&gt;(id)&lt;/code&gt; cast of a class to a protocol. This forced NimbusKit’s new &lt;a href=&quot;https://github.com/NimbusKit/swift/blob/master/TableCellFactory.swift&quot;&gt;TableCellFactory&lt;/a&gt; class to provide a non-mutable singleton instance that could be used in place of the old &lt;code&gt;(id)[NICellFactory class]&lt;/code&gt;. This had the nice side effect of eliminating duplicate code paths!&lt;/p&gt;

&lt;p&gt;The new way to create a model using the global &lt;code&gt;TableCellFactory&lt;/code&gt; is as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-swift&quot;&gt;self.model = TableModel(sections: sections, delegate: TableCellFactory.tableModelDelegate())
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;TableCellFactory&lt;/code&gt; doesn’t expose the singleton instance directly. Instead, it returns a &lt;code&gt;TableCellFactory&lt;/code&gt;-conformant object. This hides the implementation of the singleton and ensures that the global &lt;code&gt;TableCellFactory&lt;/code&gt; behaves consistenly in every setting.&lt;/p&gt;

&lt;h2 id=&quot;unable-to-use-generic-objects-as-delegates&quot;&gt;Unable to use generic objects as delegates&lt;/h2&gt;

&lt;p&gt;The compiler allows you to do so but the Objective-C runtime will not know that the protocol methods are implemented. This results in unexpected behavior at best. At worst you’ll encounter run-time crashes due to unimplemented required selectors - even though they &lt;em&gt;are&lt;/em&gt; implemented.&lt;/p&gt;

&lt;p&gt;This is likely due to the inability to declare Objective-C selectors for methods on generic classes in Swift.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-swift&quot;&gt;import Foundation
import UIKit

class DataSourceProvider &amp;lt;T: AnyObject&amp;gt; : NSObject, UITableViewDataSource {
  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&amp;gt; Int {
    return 1
  }

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&amp;gt; UITableViewCell {
    let reuseIdentifier = &quot;reuse&quot;
    if let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as? UITableViewCell {
      return cell
    }
    return UITableViewCell(style: .Default, reuseIdentifier: reuseIdentifier)
  }
}

class DelegateViewController: UITableViewController {
  let provider = DataSourceProvider&amp;lt;NSObject&amp;gt;()

  override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.dataSource = self.provider
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you attempt to display this controller the app will crash due to an “unrecognized selector sent to instance”.&lt;/p&gt;

&lt;p&gt;Making the class non-generic will allow you to use instances as delegates as expected.&lt;/p&gt;

&lt;p&gt;This has impacted the way NimbusKit’s &lt;a href=&quot;https://github.com/NimbusKit/swift/blob/master/TableActions.swift&quot;&gt;TableActions&lt;/a&gt; are implemented. Due to the above limitation, TableActions can’t take advantage of generics. &lt;em&gt;sadpanda&lt;/em&gt;&lt;/p&gt;
</description>
        <pubDate>Sat, 15 Nov 2014 17:00:00 +0000</pubDate>
        <link>http://nimbuskit.github.io//Swift-Gotchas/</link>
        <guid isPermaLink="true">http://nimbuskit.github.io//Swift-Gotchas/</guid>
      </item>
    
  </channel>
</rss>